我在我的Android应用程序中使用水平进度条,我想改变它的进度颜色(默认为黄色)。我如何使用代码(而不是XML)做到这一点?


当前回答

在Xml中添加ProgressBar

适用于SDK 21及以上版本

android:indeterminateTint="@color/red"

其他回答

水平ProgressBar使用矩形形状绘制背景,ClipDrawable从矩形形状构建的进展(主要和次要)。着色将颜色改变为某种色调。如果你想分别为这三种颜色设置目标颜色,你可以使用ProgressBar.setProgressDrawable(),如下所示:

    LayerDrawable progressBarDrawable = new LayerDrawable(
        new Drawable[]{
                new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                            new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),

                new ClipDrawable(
                            new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                    new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                            Gravity.START,
                            ClipDrawable.HORIZONTAL),

                new ClipDrawable(
                        new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                        Gravity.START,
                        ClipDrawable.HORIZONTAL)
            });

    progressBarDrawable.setId(0,android.R.id.background);
    progressBarDrawable.setId(1,android.R.id.secondaryProgress);
    progressBarDrawable.setId(2,android.R.id.progress);
    ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);

注意,在初始化LayerDrawable时,可绘制层的顺序很重要。首先绘制的应该是背景。根据我的实验,切换id是行不通的。如果你将填充设置为progressbar,那么这种方法将不起作用。如果你需要填充,那么你可以使用一个容器的进度条,如线性布局。

这不是程序化的,但我认为它可以帮助很多人。 我尝试了很多,最有效的方法是在。xml文件中添加这行到我的ProgressBar:

            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary"

所以在最后这段代码为我做了:

<ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"
            android:visibility="visible"
            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary">

该解决方案适用于API 21+

简单的使用方法:

DrawableCompat.setTint(progressBar.getIndeterminateDrawable(),yourColor)

默认值(不确定)

add

android:indeterminateTint="@color/white"

为确定

    android:progressTint="@color/color_1"

    //OR
    progressBar.getProgressDrawable().setColorFilter( PorterDuffColorFilter(Color.RED,android.graphics.PorterDuff.Mode.SRC_IN));

对于水平的ProgressBar,你也可以使用ColorFilter,就像这样:

progressBar.getProgressDrawable().setColorFilter(
    Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

注意:这将修改应用程序中所有进度条的外观。要只修改一个特定的进度条,请执行以下操作:

Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);

如果progressBar是不确定的,则使用getIndeterminateDrawable()而不是getProgressDrawable()。

因为棒棒糖(API 21),你可以设置一个进度色调:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));