76

我有一个使用 ProgressBar 类的进度条。

只是这样做:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

我需要使用如下输入值更改那个颜色:

int color = "red in RGB value".progressBar.setColor(color)

或类似的东西...

我不能使用XML 布局,因为进度条可以为用户定制。

4

14 回答 14

62

这将很有帮助,不需要做这么多的编码:)

ProgressBar spinner = new android.widget.ProgressBar(
            context,
            null,
            android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);
于 2013-02-25T06:31:34.700 回答
47

如果您需要为背景和进度条着色不同的颜色。

progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape android:shape="rectangle" >
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/green" />
            </shape>
        </clip>
    </item>
</layer-list>

它可以以编程方式分解其层列表项,并分别为它们着色:

LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);
Drawable progressDrawable = progressBarDrawable.getDrawable(1);

backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN);
progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);
于 2017-03-22T20:16:20.707 回答
44

更新

在较新版本的 Android(21 个作品)中,您只需使用setProgressTintList.

要将其设置为红色,请使用:

//bar is a ProgressBar
bar.setProgressTintList(ColorStateList.valueOf(Color.RED));
于 2015-08-14T22:11:43.373 回答
20

当我在此处找到有关某个主题的帮助但不记得链接时,我正在发布我的完整解决方案,该解决方案非常适合我的需求:

    // Draw a simple progressBar from xml
    progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

    // Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)
    String color = colorDecToHex(75, 150, 160);

    // Define a shape with rounded corners
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners,     null, null));

    // Sets the progressBar color
    pgDrawable.getPaint().setColor(Color.parseColor(color));

    // Adds the drawable to your progressBar
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    progressBar.setProgressDrawable(progress);

    // Sets a background to have the 3D effect
    progressBar.setBackgroundDrawable(Utils.getActivity().getResources()
            .getDrawable(android.R.drawable.progress_horizontal));

    // Adds your progressBar to your layout
    contentLayout.addView(progressBar);

这是将 DECIMAL 颜色值转换为 HEXADECIMAL 的代码:

public static String colorDecToHex(int p_red, int p_green, int p_blue)
{
    String red = Integer.toHexString(p_red);
    String green = Integer.toHexString(p_green);
    String blue = Integer.toHexString(p_blue);

    if (red.length() == 1)
    {
        red = "0" + red;
    }
    if (green.length() == 1)
    {
        green = "0" + green;
    }
    if (blue.length() == 1)
    {
        blue = "0" + blue;
    }

    String colorHex = "#" + red + green + blue;
    return colorHex;
}

我认为最后一种方法不是那么干净,但效果很好。

希望这会有所帮助,在这个进度条上浪费了太多时间。

于 2012-06-14T08:45:49.420 回答
16

这适用于 AppCompat:

DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);
于 2015-11-27T08:17:23.337 回答
15

可以通过在可绘制的进度条上设置颜色过滤器来为进度条着色:

Drawable drawable = progressBar.getProgressDrawable();
drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt));
于 2012-06-08T15:54:35.800 回答
14

如果您想以编程方式更改进度条的颜色,那么您复制此代码它正在 100% 工作

 mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);   
于 2017-09-22T11:09:11.573 回答
7

科特林 2021:

progressBar.indeterminateTintList = ColorStateList.valueOf(Color.WHITE)

您还可以使用 getColor() 传递资源

如果不是不确定的,您可能需要这 2 个:

setSecondaryProgressTintList

setProgressTintList
于 2021-05-02T13:29:19.740 回答
6

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

它仅适用于 API 21 以上

于 2017-02-17T02:35:39.603 回答
3

我通过drawablexml中给出了默认颜色

我以编程方式更改了它。

活动_splasg.xml

<ProgressBar
       android:id="@+id/splashProgressBar"
       android:progressDrawable="@drawable/splash_progress_drawable"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="100"
       android:progress="50"
       style="?android:attr/progressBarStyleHorizontal"
       android:layout_alignParentBottom="true" />

splash_progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid
                android:color="@android:color/transparent" />
        </shape>
    </item>

    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <solid
                    android:color="#e5c771" />
            </shape>
        </clip>
    </item>

</layer-list>

现在如何以编程方式更改 ProgressDrawable 颜色

ProgressBar splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);

Drawable bgDrawable = splashProgressBar.getProgressDrawable();
bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
splashProgressBar.setProgressDrawable(bgDrawable);

希望这会帮助你。

于 2016-03-15T09:32:05.997 回答
1

布局 = activity_main.xml:

<ProgressBar
    android:id="@+id/circle_progress_bar_middle"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:max="100"
    android:rotation="-90"
    android:indeterminate="false"
    android:progressDrawable="@drawable/my_drawable_settings2" />

在 Java 活动/片段中:

ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle);
myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1));

drawable/mipmap 文件夹中的 my_drawable_settings1.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <shape
            android:innerRadius="55dp"
            android:shape="ring"
            android:thickness="9dp"
            android:useLevel="true">

            <gradient
                android:startColor="#3594d1"
                android:endColor="@color/white"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>

其中 my_drawable_settings1 和 my_drawable_settings2.xml 有不同的颜色。

于 2017-09-12T20:13:28.663 回答
0

setColorFilter不推荐使用带有 2 个参数的另一个答案,指向使用的另一个答案LightingColorFilter对我来说都没有用,所以

val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyle).apply {

    val colorFilter = PorterDuffColorFilter(
        ContextCompat.getColor(context, R.color.yourColor),
        PorterDuff.Mode.MULTIPLY
    )

    indeterminateDrawable.colorFilter = colorFilter
}

这将以编程方式为您提供带有颜色的圆形进度条

于 2020-03-31T18:15:39.000 回答
0

试试这个:

progress_wheel.getIndeterminateDrawable().setColorFilter(Color.parseColor(getPreferences().getString(Constant.SECOND_COLOR, Constant.SECONDARY_COLOR)), android.graphics.PorterDuff.Mode.MULTIPLY);
于 2021-01-09T07:17:02.280 回答
0

试试这个代码:

   CircularProgressIndicator  circularProgressIndicator = findViewById(R.id.tenant_progress_color);
    circularProgressIndicator.setIndicatorColor(color);
    circularProgressIndicator.setTrackColor(R.color.gray);
于 2022-01-17T17:35:05.390 回答