如何在android中更改进度条的颜色

时间:2015-12-22 07:54:13

标签: android android-progressbar

我想在特定活动中更改android中进度条的颜色,而不是在整个应用程序中。

我正在使用以下进度条:

<ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerInParent="true"/>

mProgressBar = (ProgressBar) findViewById(R.id.loading);
        mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(mContext, R.color.white),
                PorterDuff.Mode.SRC_IN);

使用上面的代码更改此进度条的颜色。以上代码通过应用程序更改了所有进度条的颜色。

我想仅在此特定活动中更改此进度条的颜色。

我怎么能这样做,请帮助我。 非常感谢你的进步。

2 个答案:

答案 0 :(得分:3)

尝试设置colorFilter,如下所示:

progressBar.getProgressDrawable().setColorFilter(Color.GREEN, Mode.SRC_IN);

答案 1 :(得分:0)

比Java中的编程方法更好地使用此方法。

 <ProgressBar
                 style="?android:attr/progressBarStyleHorizontal"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:id="@+id/progressBar1"
                 android:layout_centerInParent="true"
                 android:progressDrawable="@drawable/progress_layer01"        
                 android:indeterminate="false" />

<强> progress_layer01.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="#f1f1f1"/>
        </shape>
    </item>



    <item android:id="@android:id/progress">
        <clip>
            <shape>

<solid android:color="#FF0000"/>

            </shape>

        </clip>
    </item>
</layer-list>

在您的JAVA代码中:

progressBar1.setProgress(45);