android - 以编程方式分配进度条样式 Android

标签 android xamarin.android xamarin

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

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/progressBarStyleHorizo​​ntal 那么我该怎么做呢?

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/

相关文章:

android - 设置背景后按钮变大 - 如何使其变小

android - 应用程序 ActionBar 中的后退按钮

android - Android 上长字符串的 Firebase 异常

java - 如何从 parse.com 数据库(Android)中检索数据?

xamarin.android - “PlusClass”已过时

c# - 将列表传递给 RootElement c# 中的部分

javascript - 读取android扩展文件cordova

c# - Uri.Parse 已弃用

c# - Android 位置时间戳转换为 C-Sharp DateTime

ios - 在 UIWebView 中限制重定向和抑制广告