gpt4 book ai didi

xamarin - 如何使用自定义渲染器在 Xamarin Forms 中制作带圆角的进度条

转载 作者:行者123 更新时间:2023-12-02 17:06:22 25 4
gpt4 key购买 nike

我想在 Xamarin 表单中显示带有圆角的进度条。我可以使用自定义渲染器在 iOS 中完成此操作:

class CustomProgressBarRenderer: ProgressBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
Control.ProgressTintColor = Color.FromRgb(255, 201, 74).ToUIColor();
}

public override void LayoutSubviews()
{
base.LayoutSubviews();
var X = 1.0f;
var Y = 7.0f;
CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
this.Transform = transform;
this.ClipsToBounds = true;
this.Layer.MasksToBounds = true;
this.Layer.CornerRadius = 5; // This is for rounded corners.
}
}

如何使用自定义渲染器在 Android 中实现相同的效果。这就是我想要的进度条(圆角)的外观。

enter image description here

最佳答案

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

您可以在 Android ProgressBar 上添加一个 Drawable 来实现此功能。

设置 ProgressBarDrawable\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:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="#3F8CC4" />
<stroke
android:width="0dp"
android:color="#FFFFFF"></stroke>
</shape>

在您的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;
//below is now deprecated
//var draw = Resources.GetDrawable(Resource.Drawable.bar_color);
var draw = Context.GetDrawable(Resource.Drawable.bar_color);
progressBar.ProgressDrawable = draw;
}
}
}

效果:

enter image description here

关于xamarin - 如何使用自定义渲染器在 Xamarin Forms 中制作带圆角的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com