我以编程方式生成滚动条,并且定义了一种样式来更改颜色,所以我的问题自然是:如何设置滚动条的滚动条样式?
ProgressBar progressBar = new ProgressBar(this, null, Android.Resource.Attribute.ProgressBarStyleHorizontal);
// I tried ProgressBar progressBar = new ProgressBar(this, null, Android.Resource.Drawable.myprogressbar); but it doesn't work
progressBar.Progress = 25;
linearLayout.AddView(progressBar);
我的样式路径是 android:attr/progressBarStyleHorizontal
那么我该怎么做呢?
XML 代码(必须以编程方式制作)是:
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/myprogressbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar1"
android:progress="50"
android:startColor="#ffffff"
android:endColor="#ff1997e1" />
编辑:
我把:
ProgressBar progressBar = new ProgressBar(this, null, Resource.Drawable.myprogressbar);
但现在应用程序上没有出现栏:-(
编辑:
我也测试了该代码,但没有再次出现:
var progressBar = new ProgressBar(this, null, Resource.Drawable.myprogressbar)
{
Progress = 25,
LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent),
};
linearLayout.AddView(progressBar);
当我尝试在创建进度条后设置 progressdrawable 时:
progressBar.ProgressDrawable = Resource.Drawable.myprogressbar;
我有:
Cannot implicitly convert type 'int' to 'Android.Graphics.Drawables.Drawable'. An explicit conversion exists (are you missing a cast?)
最终编辑:
这是我最后的代码:
// progressbar with custom style (Resources/Drawable/myprogress.xml)
ProgressBar progressBar = new ProgressBar(this, null, Android.Resource.Attribute.ProgressBarStyleHorizontal)
{LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent)};
progressBar.ProgressDrawable = Resources.GetDrawable(Resource.Drawable.myprogressbar);
// Compute for the percent of this usage (to assign it to the progressbar)
progressBar.Progress = 33;
linearLayout.AddView(progressBar);
最佳答案
您的 ProgressBar
未显示,因为您忘记设置 LayoutParameters
。以下代码对我有用。我尝试了各种 ProgressBar
样式。
var myLinearLayout = FindViewById<LinearLayout>(Resource.Id.myLinearLayout);
var progressBar = new ProgressBar(this, null, Android.Resource.Attribute.ProgressBarStyleLarge)
{
Progress = 25,
LayoutParameters =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent)
};
myLinearLayout.AddView(progressBar);
编辑:
要将您自己的 Drawable
传递给 progressBar.ProgressDrawable
,您需要先加载它,就像您收到的错误指示的那样。
progressBar.ProgressDrawable = Resources.GetDrawable(Resource.Drawable.myprogressbar);
关于android - 以编程方式分配进度条样式 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16046448/