169

我在 Android 上使用循环进度条。我想改变它的颜色。我在用

"?android:attr/progressBarStyleLargeInverse" 

风格。那么如何改变进度条的颜色。

如何自定义样式?此外,风格的定义是什么?

4

23 回答 23

177

适用于 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 );
于 2016-04-24T21:12:28.200 回答
171

在 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.xmlProgressBar的背景中。

像这样

<ProgressBar
  android:id="@+id/ProgressBar01" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:indeterminateDrawable="@drawable/progress"
 />
于 2011-03-17T10:14:13.713 回答
136

1.首先在resource下的drawable文件夹中创建一个xml文件

命名为“progress.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" />
于 2013-07-22T10:55:50.933 回答
67

您可以使用以下代码以编程方式更改颜色:

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);

        }
    });
于 2014-02-27T14:54:14.203 回答
38

为了补充 Dato 的答案,我发现 SRC_ATOP 是一个更可取的乘法过滤器,因为它更好地支持 alpha 通道。

ProgressBar v = (ProgressBar) findViewById(R.id.progress);
v.getIndeterminateDrawable().setColorFilter(0xFFcc0000, android.graphics.PorterDuff.Mode.SRC_ATOP);
于 2014-09-12T17:42:21.313 回答
35

android:indeterminateTint="@color/progressbar"

于 2017-03-18T16:48:09.077 回答
18

样式.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"/>
于 2017-03-08T07:26:39.970 回答
13

这对我有用。

<ProgressBar
android:id="@+id/ProgressBar01" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/black"/>
于 2017-10-16T12:51:38.973 回答
12

要自定义圆形 ProgressBar,我们需要创建一个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" />
于 2015-06-23T06:22:51.767 回答
10

检查这个答案

对我来说,这两条线必须在那里才能工作并改变颜色:

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

PS:但它仅适用于 android 21

于 2019-07-08T14:47:10.147 回答
9

对我来说,主题不适用于accentColor。但它确实适用于 colorControlActivated

    <style name="Progressbar.White" parent="AppTheme">
        <item name="colorControlActivated">@color/white</item>
    </style>

  <ProgressBar
        android:layout_width="@dimen/d_40"
        android:layout_height="@dimen/d_40"
        android:indeterminate="true"
        android:theme="@style/Progressbar.White"/>
于 2017-08-01T13:51:44.107 回答
8
<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>


<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="250dp"
    android:theme="@style/progressColor"
    android:layout_height="250dp"
    android:layout_centerInParent="true" />
于 2020-07-19T08:26:14.100 回答
6

使用材料组件库,您可以使用CircularProgressIndicator以下属性:

  • indicatorColor

  • trackColor

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

在此处输入图像描述

于 2021-03-23T18:17:37.657 回答
4

尝试使用样式并将 colorControlActivated 设置为所需的 ProgressBar 颜色。

<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
    <item name="colorControlActivated">@color/COLOR</item>
</style>

然后将 ProgressBar 的主题设置为新样式。

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/progressColor"
 />
于 2020-02-05T01:32:17.667 回答
4

为了补充Muhamed Riyas M 的最高投票答案:

更快的旋转

android:toDegrees="1080"

较薄的环

android:thicknessRatio="16"

浅白色

android:endColor="#80ffffff"
于 2018-10-20T02:22:38.477 回答
4

添加到您的活动主题,项目colorControlActivated,例如:

    <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
            ...
            <item name="colorControlActivated">@color/rocket_black</item>
            ...
    </style>

将此样式应用于清单中的 Activity:

<activity
    android:name=".packege.YourActivity"
    android:theme="@style/AppTheme.NoActionBar"/>
于 2017-08-02T15:00:17.023 回答
3

您可以使用一种样式来更改进度的颜色,如下所示

 <style name="AppTheme.anyName">
        <item name="colorAccent">YOUR_COLOR</item>
    </style>

并在 ProgressBar 中使用它,如下所示

<ProgressBar
            style="?android:attr/progressBarStyle"
            android:theme="@style/AppTheme.WhiteAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/dimen_30"
        />

希望能帮助到你。

于 2018-09-04T13:59:12.100 回答
2

您可以很容易地使用自定义对话框执行此操作,重用其他答案中的 xml:

<?xml version="1.0" encoding="utf-8"?>

<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/oceanBlue"
        android:startColor="@android:color/transparent"
        android:type="sweep"
        android:useLevel="false" />

</shape>

只需这样做:

public static class ModifiedProgressDialog extends ProgressDialog {
    public ModifiedProgressDialog(Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        setIndeterminateDrawable(getContext().getResources().getDrawable(R.drawable.blue_progress));
    }
}
于 2016-11-15T18:11:00.457 回答
1

对于所有想知道如何提高自定义进度速度的人

  <?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="1080"><!--HERE YOU COULD INCREASE SPEED BY SETTING TODEGRESS(1080 is 3 loops instead of 1 in same amt of time)-->
<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="#447a29"
        android:angle="0"
         />
</shape>

于 2018-02-19T06:55:40.223 回答
0

android:indeterminateDrawable="@drawable/progress_custom_rotate"

使用以下代码自定义圆环进度条

复制下面的代码并在 Drawable 文件夹中创建“progress_custom_rotate.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="1080">

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

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

        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#4c737373" android:centerColor="#4c737373"
            android:centerY="0.50" android:endColor="#ffffd300" />

    </shape>

</rotate>
于 2020-04-19T21:58:32.890 回答
0

它从你的 Res/Values/Colors.xml -> colorAccent 中获取颜色值,如果你改变它,你的progressBar 颜色也会改变。

于 2020-01-16T10:47:37.410 回答
0

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/black</item>
        <item name="colorPrimaryDark">@color/white</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="progressColor" parent="Widget.AppCompat.ProgressBar">
        <item name="colorControlActivated">@color/black</item>
    </style>
</resources>
<ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:theme="@style/progressColor"
            app:layout_constraintBottom_toBottomOf="@+id/textView3"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="@+id/textView3" />

于 2021-03-08T12:02:46.597 回答
-1

您可以使用以下代码更改进度条颜色:

progressBar.getProgressDrawable().setColorFilter(
    getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);
于 2016-08-16T07:26:34.223 回答