如何更改进度条的进度颜色?

19

我为我的应用程序创建了这个进度条,但是我无法获得那种黄色橙色的颜色,它似乎会变成红色或蓝色之类的颜色。

<ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="250sp"
        android:layout_height="wrap_content"
        android:progress="2"
        android:layout_marginTop="82dp"
        android:max="3"
        android:indeterminate="false"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

以上是我的进度条XML代码。

有人能帮忙吗?:)

非常感谢您的提前帮助!:)


1
可能是Android更改水平进度条颜色的重复问题。 - earthw0rmjim
8个回答

53
如果Android版本是5.0或更高版本,你只需要设置:

android:indeterminateTint="@color/BLACK"
android:indeterminateTintMode="src_in"

对于较低版本,我使用这个

mProgressBar.getIndeterminateDrawable().setColorFilter(getResources()
.getColor(R.color.primary_color),PorterDuff.Mode.SRC_IN);

13
在您的drawable文件夹中创建一个名为custom_progress_bar.xml的新XML文件,并将以下内容粘贴到其中。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
        <clip>
            <shape>
                <solid android:color="#000000" />
            </shape>
        </clip>
    </item>
    <!-- Define the progress properties like start color, end color etc -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#FFFFFF" />
            </shape>
        </clip>
    </item>

</layer-list>

在你的<ProgressBar>中添加此属性:

android:progressDrawable="@drawable/custom_progress_bar"

将这两个颜色更改为您喜欢的颜色。我放置了#FFFFFF#000000,它们分别是白色和黑色。


1
亲爱的Vuko,你的方法非常有效!感谢你的帮助! :) - AmateurProgrammer
我想以编程的方式达到同样的效果。在我的情况下,我想通过使用十六进制值来改变进度条的颜色而不改变背景颜色。需要帮助。 - Navjot
我想到的是您可以使用不同的颜色创建相同的可绘制对象,并从代码中替换进度可绘制对象。如果需要动态选择颜色(并非始终相同),则会更加困难,如果我想到解决方案,我会告诉您:D @NavjotBedi - Vucko

6

请前往您的 styles.xml 并将 colorAccent 值从您的基础应用程序主题更改,例如:
<item name="colorAccent"> {COLOR} </ item>
我正在使用minSdkVersion 15,这对我有效。


3
只有当你不介意所有地方的颜色都可能改变为这种颜色时,这才是一个好的解决方案。ColorAccent 是一个非常重要的属性,应该谨慎更改。请查看 这个链接 - Vucko
这个方案看起来比我的解决方案更简单,只需添加这一行代码就可以了,但它有一些注意事项。在使用之前请小心。 - Vucko

5
如果您想更改ProgressBar的颜色,不是通过drawable,您可以将这个样式作为ProgressBar的主题使用。
<style name="ProgressBar" parent="Widget.AppCompat.ProgressBar">
    <item name="colorAccent">@color/white_primary</item>
</style>

3
如果 android:indeterminateTint 不起作用,请尝试以下方法:
<ProgressBar
    android:id="@+id/progress_bar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:progressTint="@color/Red" <--------------------------
    android:progressBackgroundTint="@color/Blue" <---------------
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:progress="2"
    android:layout_marginTop="82dp"
    android:max="3"
    android:indeterminate="false"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true" />

2

尝试这个并添加您自定义的颜色

Progressbar mBar= (ProgressBar) findViewById(R.id.spinner);
mBar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#80DAEB"),
            android.graphics.PorterDuff.Mode.MULTIPLY);

希望这有所帮助。

2

供日后参考:

对于API 21+,我们可以直接在XML中使用:

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

1
在 style.xml 中设置主题,像这样:
 <style name="ProgressBar" parent="Widget.AppCompat.ProgressBar">
    <item name="colorAccent">@color/dark_blue</item>
</style>

然后在progressBar中使用这个样式作为“theme”,就像这样:

<ProgressBar
    android:id="@+id/progressBar"
    android:theme="@style/ProgressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="440dp"
    app:layout_constraintEnd_toEndOf="@+id/splashTitle"
    app:layout_constraintHorizontal_bias="0.493"
    app:layout_constraintStart_toStartOf="@+id/splashTitle"
    app:layout_constraintTop_toBottomOf="@+id/splashTitle" />

编程愉快


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接