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


当前回答

对于水平的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));

其他回答

这不是程序化的,但我认为它可以帮助很多人。 我尝试了很多,最有效的方法是在。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+

在修改默认进度条的外观时遇到了同样的问题。这里有一些更多的信息,希望能帮助到人们:)

The name of the xml file must only contain characters: a-z0-9_. (ie. no capitals!) To reference your "drawable" it is R.drawable.filename To override the default look, you use myProgressBar.setProgressDrawable(...), however you need can't just refer to your custom layout as R.drawable.filename, you need to retrieve it as a Drawable:Resources res = getResources(); myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename); You need to set style before setting progress/secondary progress/max (setting it afterwards for me resulted in an 'empty' progress bar)

相信我,最简单的解决方法就是把这个粘贴到progressBar中:

android:indeterminateTint="@android:color/white"

使用android.support.v4.graphics.drawable.DrawableCompat:

            Drawable progressDrawable = progressBar.getIndeterminateDrawable();
            if (progressDrawable  != null) {
                Drawable mutateDrawable = progressDrawable.mutate();
                DrawableCompat.setTint(mutateDrawable, primaryColor);
                progressBar.setProgressDrawable(mutateDrawable);
            }

发布了关于PaulieG的答案的补充信息,因为ateiob让我解释一些事情…


我可以说,在ProgressBar代码中存在(或者至少是,当我查看当前版本的Android源代码时)一个错误/问题/优化,忽略了将进度设置为已经存在的值的尝试。

例如,如果progress = 45,而你试图将其设置为45,代码将什么都不做,也不会重绘进度。

调用ProgressBar.setProgressDrawable()后,你的进度条将是空白的(因为你改变了可绘制的部分)。

这意味着你需要设置进度,并重新绘制它。但是如果您只是将进度设置为保留值,它将什么也不做。

您必须先将其设置为0,然后再次设置为“旧”值,然后条将重新绘制。


总结一下:

保留“旧的”进度值 更新可绘制/颜色(使栏空白) 将进度重置为0(否则下一行不执行任何操作) 将进度重置为“旧”值(修复条) 无效


下面是一个方法,我有这样做:

protected void onResume()
{
    super.onResume();
    progBar = (ProgressBar) findViewById(R.id.progress_base);

    int oldProgress = progBar.getProgress();

    // define new drawable/colour
    final float[] roundedCorners = new float[]
        { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
        roundedCorners, null, null));
    String MyColor = "#FF00FF";
    shape.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);
    progBar.setProgressDrawable(clip);

    progBar.setBackgroundDrawable(getResources().getDrawable(
        android.R.drawable.progress_horizontal));

    // work around: setProgress() ignores a change to the same value
    progBar.setProgress(0);
    progBar.setProgress(oldProgress);

    progBar.invalidate();
}

至于HappyEngineer的解决方案,我认为它是一个类似的解决方案,手动设置“进度”偏移量。无论哪种情况,上面的代码都应该适用于您。