How to make a progress bar with rounded corners in Xamarin Forms using Custom Renderer

雨燕双飞 提交于 2019-11-29 05:11:49

How to make a progress bar with rounded corners in Xamarin forms

You could add a Drawable on your android ProgressBar to implement this feature.

Set the style for your ProgressBar , Drawable\bar_color.xml :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--set progressbar backgound style-->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progress_bar_background" />

    <!--set progressbar schedule style-->
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/shape_progressbar_progress"
            android:scaleWidth="100%" />
    </item>

</layer-list>

Drawable\progress_bar_background.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <solid android:color="#FFCBC2C2" />
</shape>

Drawable\shape_progressbar_progress.xml :

<corners android:radius="20dp" />
   <solid android:color="#3F8CC4" />
   <stroke
       android:width="0dp"
       android:color="#FFFFFF"></stroke>
</shape>

Using it in your CustomProgressBarRenderer :

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        if(Control != null)
        {
            var progressBar = Control as Android.Widget.ProgressBar;
            var draw = Resources.GetDrawable(Resource.Drawable.bar_color);
            progressBar.ProgressDrawable = draw;
        }
    }
}

Effect :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!