如何在圆形进度条中改变颜色?

178

我在Android上使用圆形进度条,希望改变它的颜色。我正在使用

"?android:attr/progressBarStyleLargeInverse" 

如何更改进度条的颜色?需要怎样自定义样式呢?此外,“style”在这里的定义是什么?

23个回答

188

对于API 21及更高版本,只需设置indeterminateTint属性。例如:

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

为了支持API 21之前的设备:

mProgressSpin.getIndeterminateDrawable()
                .setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN );

4
带有 style="?android:attr/progressBarStyle" 的语句不起作用。 - user25
如果我有一个颜色数组,并且我想将颜色设置为每个进度条上的颜色数组。 - Prince Dholakiya
这对我来说很好用,只是改变了进度条环的颜色 :) - Gastón Saillén
感谢您强调API方面。 - JamisonMan111
1
如果我将这个颜色放在旋转的切割环上,它会将整个环都着色,因此它变成了一个完整的环,所以你看不到它在移动。 - PPP

175

将以下内容放在res/drawable文件夹中:

progress.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:fromDegrees="0"
      android:toDegrees="360">

    <shape 
        android:shape="ring" 
        android:innerRadiusRatio="3"
        android:thicknessRatio="8" 
        android:useLevel="false">

    <size 
        android:width="76dip" 
        android:height="76dip" />

    <gradient 
        android:type="sweep" 
        android:useLevel="false"
        android:startColor="#447a29" 
        android:endColor="#00ffffff"
        android:angle="0"/>

    </shape>

</rotate> 

根据您的选择设置startColorendColor

现在将progress.xml设置为ProgressBar的背景。

像这样

<ProgressBar
  android:id="@+id/ProgressBar01" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:indeterminateDrawable="@drawable/progress"
 />

1
当我使用自定义对话框作为列表视图标题时,它无法正常工作。是否需要另一种方法? - Pankaj Kumar
22
这将创建一个绿色的环形,但实际的 ProgressBar 的旋转图标仍然可见。 - mdupls
50
另一个回答指出,应该使用“android:indeterminateDrawable="@drawable/progress"”来设置背景,而不是使用“android:background”。请注意修改内容以使之更通俗易懂,但不要改变原意。 - baekacaek
谢谢,它完美地运行了。@Chirag 我该如何让它运行得更快一些? - Piyush Agarwal
1
使用不同的 android:startColor="#447a29" 和 android:endColor="#227a29" 值,以避免形成静态圆形。 - Abhishek Sengupta
显示剩余5条评论

139

1.在资源文件夹下的drawable文件夹中创建一个名为"progress.xml"的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >

    <size
        android:height="76dip"
        android:width="76dip" />

    <gradient
        android:angle="0"
        android:endColor="color/pink"
        android:startColor="@android:color/transparent"
        android:type="sweep"
        android:useLevel="false" />

    </shape>

</rotate>

2.然后使用以下片段制作进度条

<ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/relativeLayout1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"
    android:indeterminate="true"
    android:indeterminateDrawable="@drawable/progress" />

嗨,Muhamed,我正在使用自定义进度轮,我尝试在运行时更改进度轮的颜色。我已经尝试了一次,当我点击按钮时,进度开始。我将颜色设置为绿色。当进度达到100%时,我希望它再次开始,但这次它的颜色应该是红色。但是,我无法做到这一点...如果可能,请帮助我。 - harikrishnan
1
谢谢。它百分之百有效。将progress.xml设置为android:indeterminateDrawable。 - Leo Nguyen
当我尝试这个时,它创建了一个圆的粗边框。有什么办法可以减少边框? - Tejas
嗨,Tejas,尝试更改progress.xml中的android:thicknessRatio="8"这一行。您可以尝试不同的值。希望它能起作用。 - Muhamed Riyas M
<shape>标签中删除android:useLevel="false"后,我的旧版Android设备开始旋转。谢谢。 - Shashanth
显示剩余2条评论

68

你可以使用以下代码来通过编程方式改变颜色:

ProgressBar v = (ProgressBar) findViewById(R.id.progress);
v.getIndeterminateDrawable().setColorFilter(0xFFcc0000,
                        android.graphics.PorterDuff.Mode.MULTIPLY);

如果你想改变ProgressDialog的进度条颜色,可以使用以下方法:

mDilog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ProgressBar v = (ProgressBar)mDilog.findViewById(android.R.id.progress);
            v.getIndeterminateDrawable().setColorFilter(0xFFcc0000,
                    android.graphics.PorterDuff.Mode.MULTIPLY);

        }
    });

嘿,我该如何将ProgressDialog的进度条与Progress.xml连接起来? - Meghal Agrawal
如果我有一个颜色数组,并且我想将颜色设置为每个进度条上的颜色数组。 - Prince Dholakiya

39

补充Dato的回答,我发现SRC_ATOP是比multiply更好的过滤器,因为它更好地支持alpha通道。

ProgressBar v = (ProgressBar) findViewById(R.id.progress);
v.getIndeterminateDrawable().setColorFilter(0xFFcc0000, android.graphics.PorterDuff.Mode.SRC_ATOP);

1
在2.3.x版本中,这将为您提供一个实心填充的圆形,其中MULTIPLY保留透明度。 - dvs
如果我有一个颜色数组,并且我想将颜色设置为每个进度条上的颜色数组。 - Prince Dholakiya
使用兼容版本:progressBar.indeterminateDrawable.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(0xffffff, BlendModeCompat.SRC_ATOP) - Guy Or

35

android:indeterminateTint="@color/progressbar"


只需在进度条中使用上述属性:indeterminateTint=您的颜色。 - Bhupinder Singh
最佳答案并且它将支持所有版本。 - Pankaj Kumar
这就是你需要的,而不是那些疯狂的答案。虽然其他人可能会从完整的代码块中受益。 - StarWind0
7
"indeterminateTint" 可在 API 21 或更高版本中使用。如果这对您有用,那太棒了! - Andy Weinstein

19

样式.xml

<style name="CircularProgress" parent="Theme.AppCompat.Light">
    <item name="colorAccent">@color/yellow</item>
</style>

  <ProgressBar
        android:layout_width="@dimen/d_40"
        android:layout_height="@dimen/d_40"
        android:indeterminate="true"
        style="@style/Widget.AppCompat.ProgressBar"
        android:theme="@style/CircularProgress"/>

我认为这个链接会对你有所帮助:https://github.com/Todd-Davies/ProgressWheel/blob/master/src/com/todddavies/components/progressbar/ProgressWheel.java - Hardik Vasani

15

通过 Material Components 库,您可以使用以下属性的 CircularProgressIndicator

  • indicatorColor

  • trackColor

      <com.google.android.material.progressindicator.CircularProgressIndicator
          android:indeterminate="true"
          app:trackColor="@color/..."
          app:indicatorSize="90dp"
          app:indicatorColor="@color/..."
          />

这里输入图片描述


2
这个应该是2021年目前为止被接受的。 - Zohab Ali

15

对我来说,这很有效。

<ProgressBar
android:id="@+id/ProgressBar01" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/black"/>

5
API级别21及以上可用。 - viplezer

11

要自定义圆形进度条,我们需要创建一个indeterminateDrawable来显示自定义进度条。

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1">

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false">

        <size
            android:height="48dip"
            android:width="48dip" />

        <gradient
            android:centerColor="#f96047"
            android:centerY="0.50"
            android:endColor="#fb9c8d"
            android:startColor="#f72d0c"
            android:type="sweep"
            android:useLevel="false" />

    </shape>

</rotate>

我们需要像下面这样在进度条中包含drawable:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    style="?android:attr/progressBarStyleLarge"
    android:visibility="visible"
    android:progressDrawable="@drawable/widget_progressbar"
    android:indeterminateDrawable="@drawable/widget_progressbar"
    android:layout_height="wrap_content" />

shape之前缺少rotate标签。如果没有它,进度条将是静态的。 - Renan Bandeira
@RenanBandeira 如果答案正确,您可以改进它。 - Akshay Mukadam

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