1

我正在以编程方式生成滚动条,并且我已经定义了一种更改颜色的样式,所以我的问题自然是:如何设置滚动条的滚动条样式?

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;

我有:

无法将类型“int”隐式转换为“Android.Graphics.Drawables.Drawable”。存在显式转换(您是否缺少演员表?)

最终编辑:

这是我的最后一个代码:

// 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);
4

1 回答 1

2

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);
于 2013-04-17T20:34:54.243 回答