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

Whi*_*erK 429 android android-progressbar

我在Android应用程序中使用水平进度条,我想更改其进度颜色(默认情况下为黄色).我怎么能用code(不是XML)呢?

Tor*_*ier 464

对于水平ProgressBar,您也可以使用a ColorFilter,如下所示:

progressBar.getProgressDrawable().setColorFilter(
    Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)

红色ProgressBar使用彩色滤光片

注意:这会修改应用中所有进度条的外观.要仅修改一个特定的进度条,请执行以下操作:

Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);
Run Code Online (Sandbox Code Playgroud)

如果progressBar不确定,则使用getIndeterminateDrawable()而不是getProgressDrawable().

从Lollipop(API 21)开始,您可以设置进度色调:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
Run Code Online (Sandbox Code Playgroud)

红色ProgressBar使用进度色调

  • 如果使用不确定的进度,请不要忘记对`getIndeterminateDrawable`执行相同的操作. (39认同)
  • @resus我有同样的问题,酒吧是完全红色的,没有看到任何进展.但我解决了它将Mode.SRC_IN更改为Mode.MULTIPLY. (33认同)
  • 怎么不改变背景颜色?我只想改变进度颜色. (8认同)
  • 我想使用这个答案,因为在我看来它是最简单的一个,但我的进度条完全是红色的,与进度值无关.你确定要使用的模式吗? (7认同)
  • 请注意,此模式中的"模式"是指android.graphics.PorterDuff.Mode (6认同)
  • 另请注意,执行此操作将修改应用中所有进度条的drawable.要仅对特定的一个执行此操作,请在drawable上调用mutate(),然后在其上设置颜色过滤器,然后在progressBar上调用set*Drawable. (3认同)
  • 您可以使用Color.parseColor("#12ddfe")而不是Color.RED. (3认同)
  • 很棒的建议!`progressBar.getProgressDrawable().setColorFilter(Color.RED,Mode.SRC_IN);`无需修改即可使用.并且使用模式"MULTIPLY"进度可绘制会使几个阴影变暗. (2认同)

FFi*_*ich 308

这不是以编程方式,但我认为它可以帮助很多人!我尝试了很多,最有效的方法是将这些行添加到.xml文件中的ProgressBar中:

            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary"
Run Code Online (Sandbox Code Playgroud)

所以最后这段代码为我做了:

<ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"
            android:visibility="visible"
            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary">
Run Code Online (Sandbox Code Playgroud)

此解决方案适用于API 21+

  • 要求最低API级别21 (38认同)
  • 当 Android 从 4.x 最低版本转移时的最佳答案 (5认同)

Ale*_*voy 280

对不起,这不是答案,但是什么推动了从代码设置它的要求?.setProgressDrawable如果它被正确定义,应该工作

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="@color/progress_start"
                android:endColor="@color/progress_end"
                android:angle="270" 
            />
        </shape>
    </clip>
</item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

  • 它没有向我显示任何颜色变化.请告诉我们的颜色. (7认同)
  • 原因是因为我正在动态创建进度条,并根据用户请求设置其颜色.由于我通常使用代码来构建我的GUI屏幕和组件,我不熟悉附加的XML,我不知道什么是层列表(虽然我猜你正在构建基于几个层的进度条..).如果我确实想要使用您附加的XML - 我应该将它放在项目的文件夹中,还有什么需要根据XML设置创建进度条吗?谢谢. (4认同)
  • 注意 - 文件是SDK中的文件的副本.我在这里删除了版权.如果你查看res/drawable文件夹,你会看到我发布的确切内容 - 颜色设置为黄色渐变,而不是自定义颜色. (2认同)

jha*_*tar 221

对于我的不确定进度条(微调器),我只是在drawable上设置了一个颜色过滤器.效果很好而且只有一条线.

将颜色设置为红色的示例:

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

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 我最喜欢你的答案.为了我的工作虽然我必须这样做:myProgressBarSpinner.getIndeterminateDrawable().setColorFilter(new LightingColorFilter(0xFF000000,foregroundColorDesired)); (14认同)
  • 另请注意,执行此操作将修改应用中所有进度条的drawable.要仅对特定的一个执行此操作,请在drawable上调用mutate(),然后在其上设置颜色过滤器,然后在progressBar上调用setIntederminateDrawable. (4认同)
  • 注意:那些使用它并想知道为什么它们的颜色看起来与它们放入的十六进制字符串完全不同的人,它上面有一个多色滤镜.将`android.graphics.PorterDuff.Mode.MULTIPLY`更改为`PorterDuff.Mode.SRC_IN`,您将获得确切的十六进制颜色. (3认同)
  • 我用作:progressBar =(ProgressBar)findViewById(R.id.progressBar); progressBar.getIndeterminateDrawable()。setColorFilter(ContextCompat.getColor(this,android.R.color.white),android.graphics.PorterDuff.Mode.MULTIPLY); (3认同)

kir*_*403 172

这是一个老问题,但这里没有提到使用主题.如果您的默认主题正在使用AppCompat,您ProgressBar的颜色将是colorAccent您已定义的颜色.

改变colorAccent也会改变你ProgressBar的颜色,但是变化也反映在多个地方.所以,如果你想要一个特定的不同颜色,PregressBar你可以通过应用主题来做到这一点ProgressBar:

所以它看起来像这样:

<ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:theme="@style/AppTheme.WhiteAccent" />
Run Code Online (Sandbox Code Playgroud)

所以你只是换一个colorAccent特定的ProgressBar.

注意:使用style不起作用.你android:theme只需要使用.您可以在此处找到更多主题用途:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

  • 最后!谢谢,似乎没有多少开发人员使用AppCompat,很多用户只是使用API​​ 21+解决方案获得投票答案,并不是很好,他们应该支持更多的设备/操作系统 (17认同)

Ras*_*iri 42

所有API

如果使用所有API只是在样式中创建主题

style.xml

<resources>

    //...

    <style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">@color/blue</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

并在使用过程中

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />
Run Code Online (Sandbox Code Playgroud)

API等级21及更高

如果在API级别21及更高版本中使用,只需使用此代码:

<ProgressBar
   //...
   android:indeterminate="true"
   android:indeterminateTintMode="src_atop"
   android:indeterminateTint="@color/secondary"/>
Run Code Online (Sandbox Code Playgroud)


小智 34

根据一些建议,您可以指定一个形状并使用颜色进行剪裁,然后进行设置.我以编程方式工作.这是我怎么做的..

首先确保导入drawable库..

import android.graphics.drawable.*;

然后使用类似下面的代码;

ProgressBar pg = (ProgressBar)row.findViewById(R.id.progress);
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
String MyColor = "#FF00FF";
pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
pg.setProgressDrawable(progress);   
pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
pg.setProgress(45);
Run Code Online (Sandbox Code Playgroud)


MOH*_*H3N 29

这对我有用:

<ProgressBar
 android:indeterminateTint="#d60909"
 ... />
Run Code Online (Sandbox Code Playgroud)

  • 仅用于API级别21及更高级别 (10认同)

AI *_*kil 28

相信我,最简单的解决方案就是将其粘贴到 ProgressBar 中:

android:indeterminateTint="@android:color/white"
Run Code Online (Sandbox Code Playgroud)


the*_*ife 26

如果不确定:

((ProgressBar)findViewById(R.id.progressBar))
    .getIndeterminateDrawable()
    .setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)


Hen*_*usa 23

如今在2016年,我发现一些前Lollipop设备不符合colorAccent设置,因此我对所有API的最终解决方案现在如下:

// fixes pre-Lollipop progressBar indeterminateDrawable tinting
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

    Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
    mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
} else {
    mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
}
Run Code Online (Sandbox Code Playgroud)

对于奖励积分,它不使用任何弃用的代码.试试吧!


Deb*_*osh 20

这就是我所做的.工作.

进度条:

<ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:indeterminateDrawable="@drawable/progressdrawable"
           />
Run Code Online (Sandbox Code Playgroud)

progressdrawable.xml:
这里使用渐变来根据需要改变颜色.并且android:toDegrees ="X"增加X的值,进度条快速旋转.减少并且旋转缓慢.根据您的需要定制.

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

            <shape
                android:innerRadius="20dp"
                android:shape="ring"
                android:thickness="4dp"
                android:useLevel="false" >
                <size
                    android:height="48dp"
                    android:width="48dp" />

                <gradient
                    android:centerColor="#80ec7e2a"
                    android:centerY="0.5"
                    android:endColor="#ffec7e2a"
                    android:startColor="#00ec7e2a"
                    android:type="sweep"
                    android:useLevel="false" />
            </shape>

        </rotate>
Run Code Online (Sandbox Code Playgroud)

样品: 在此输入图像描述


mug*_*hil 19

对于 SDK ver 21 及更高版本

android:indeterminateTint="@color/orange"
Run Code Online (Sandbox Code Playgroud)

在 XML Works 中对我来说很容易。


pyk*_*yko 18

在修改默认进度条的外观时遇到同样的问题.这里有一些有希望帮助人们的信息:)

  • xml文件的名称必须只包含字符:( a-z0-9_.即没有大写字母!)
  • 引用你的"drawable"就是这样 R.drawable.filename
  • 要覆盖默认外观,您可以使用myProgressBar.setProgressDrawable(...),但是您不能仅仅引用自定义布局R.drawable.filename,您需要将其检索为Drawable:
    Resources res = getResources();
    myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
    
    Run Code Online (Sandbox Code Playgroud)
  • 您需要在设置进度/次要进度/最大值之前设置样式(之后为我设置导致"空"进度条)


Nas*_*sib 18

对于默认( 不确定

添加

android:indeterminateTint="@color/white"
Run Code Online (Sandbox Code Playgroud)

对于确定的

    android:progressTint="@color/color_1"

    //OR
    progressBar.getProgressDrawable().setColorFilter( PorterDuffColorFilter(Color.RED,android.graphics.PorterDuff.Mode.SRC_IN));
Run Code Online (Sandbox Code Playgroud)


Hen*_*usa 15

在这个答案中可能有一件事没有提到:

如果您的主题是继承的Theme.AppCompat,ProgressBar则将采用您"colorAccent"在主题中定义的颜色.

所以,使用..

<item name="colorAccent">@color/custom_color</item>

..将自动调整ProgressBar的颜色到@color/custom_color.


小智 14

我是如何在水平ProgressBar中完成的:

    LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
    Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
    progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)

  • 我做了同样的事情,加上背景颜色变化:layerDrawable.findDrawableByLayerId(android.R.id.background).setColorFilter(trackColor,PorterDuff.Mode.SRC); (2认同)
  • 这个答案以及 @KamenDobrev 的评论是唯一真正做到了我想要的。当我仅将颜色过滤器设置为可绘制进度时,背景颜色也会发生变化。 (2认同)

小智 14

您可以尝试更改样式,主题或使用android:indeterminateTint ="@ color/yourColor"在任何地方,但只有一种方法可以在任何Android SKD版本上使用:

如果进度条不是不确定的,请使用:

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );
Run Code Online (Sandbox Code Playgroud)

如果进度条不确定,请使用:

progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );
Run Code Online (Sandbox Code Playgroud)

Android真是太乱了!


pad*_*deg 13

android:progressTint="#ffffff" 
Run Code Online (Sandbox Code Playgroud)

  • 不错的尝试,但仅适用于API 21+ (2认同)

Nav*_*mad 12

最简单的解决方案如果要更改布局xml文件中的颜色,请使用以下代码并使用indeterminateTint属性获取所需的颜色.

    <ProgressBar
      android:id="@+id/progressBar"
      style="?android:attr/progressBarStyle"
      android:layout_width="wrap_content"
      android:indeterminate="true"
      android:indeterminateTintMode="src_atop"
      android:indeterminateTint="#ddbd4e"
      android:layout_height="wrap_content"
      android:layout_marginBottom="20dp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" />
Run Code Online (Sandbox Code Playgroud)


S.J*_*ved 11

这个解决方案对我有用:

<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"/>
Run Code Online (Sandbox Code Playgroud)


小智 10

更改进度条的前景色和背景色最简单的方法是

<ProgressBar
                        style="@android:style/Widget.ProgressBar.Horizontal"
                        android:id="@+id/pb_main"
                        android:layout_width="match_parent"
                        android:layout_height="8dp"
                        android:progress="30"
                        android:progressTint="#82e9de"
                        android:progressBackgroundTint="#82e9de"
                        />
Run Code Online (Sandbox Code Playgroud)

只需添加

                        android:progressTint="#82e9de" //for foreground colour
                        android:progressBackgroundTint="#82e9de" //for background colour
Run Code Online (Sandbox Code Playgroud)


Joh*_*ohn 9

要更改水平 ProgressBar 颜色(在 kotlin 中):

fun tintHorizontalProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.progressTintList = ColorStateList.valueOf(color)
    } else{
        val layerDrawable = progress.progressDrawable as? LayerDrawable
        val progressDrawable = layerDrawable?.findDrawableByLayerId(android.R.id.progress)
        progressDrawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
    }
}
Run Code Online (Sandbox Code Playgroud)

要更改不确定的 ProgressBar 颜色:

fun tintIndeterminateProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.indeterminateTintList = ColorStateList.valueOf(color)
    } else {
        (progress.indeterminateDrawable as? LayerDrawable)?.apply {
            if (numberOfLayers >= 2) {
                setId(0, android.R.id.progress)
                setId(1, android.R.id.secondaryProgress)
                val progressDrawable = findDrawableByLayerId(android.R.id.progress).mutate()
                progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它最终通常为棒棒糖前的进度条着色

api 19 的有色进展


Ami*_*emi 8

只需使用:

DrawableCompat.setTint(progressBar.getIndeterminateDrawable(),yourColor)
Run Code Online (Sandbox Code Playgroud)

  • 这真的很简单,而且看起来效果很好!谢谢 (3认同)

Cal*_*lin 7

还有一点,主题解决方案确实有效,如果你继承了一个基本主题,那么对于app compact,你的主题应该是:

<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/custom</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在进度条主题中设置它

<ProgressBar
    android:id="@+id/progressCircle_progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:theme="@style/AppTheme.Custom"
    android:indeterminate="true"/>
Run Code Online (Sandbox Code Playgroud)


Moh*_*ish 7

只需使用:

PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
    mode = PorterDuff.Mode.MULTIPLY;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
    progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(Color.RED));
} else {
    Drawable progressDrawable;
    progressDrawable = (progressBar.isIndeterminate() ? progressBar.getIndeterminateDrawable() : progressBar.getProgressDrawable()).mutate();
    progressDrawable.setColorFilter(context.getResources().getColor(Color.RED), mode);
    progressBar.setProgressDrawable(progressDrawable);
}
Run Code Online (Sandbox Code Playgroud)


Ric*_*ier 6

发布以添加关于PaulieG答案的信息,因为ateiob让我解释一下......


我可以说,在编写本文时,当我查看当前版本的Android源代码时,(或者至少是代码中的错误/问题/优化)ProgressBar忽略了将进度设置为值的尝试已经在了.

  • 即如果progress = 45,并且您尝试将其设置为45,则代码将不执行任何操作,并且不会重绘进度.

通话后ProgressBar.setProgressDrawable(),您的进度条将为空(因为您更改了可绘制部分).

这意味着您需要设置进度并重新绘制.但是,如果您只是将进度设置为保留值,它将不执行任何操作.

您必须先将其设置为0,然后再将其设置为"旧"值,并且该栏将重绘.


总结一下:

  • 保留"旧"进度值
  • 更新drawable/color(使条形空白)
  • 将进度重置为0(否则下一行不执行任何操作)
  • 将进度重置为"旧"值(修复栏)
  • 废止

以下是我的方法:

protected void onResume()
{
    super.onResume();
    progBar = (ProgressBar) findViewById(R.id.progress_base);

    int oldProgress = progBar.getProgress();

    // define new drawable/colour
    final float[] roundedCorners = new float[]
        { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
        roundedCorners, null, null));
    String MyColor = "#FF00FF";
    shape.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);
    progBar.setProgressDrawable(clip);

    progBar.setBackgroundDrawable(getResources().getDrawable(
        android.R.drawable.progress_horizontal));

    // work around: setProgress() ignores a change to the same value
    progBar.setProgress(0);
    progBar.setProgress(oldProgress);

    progBar.invalidate();
}
Run Code Online (Sandbox Code Playgroud)

至于HappyEngineer的解决方案,我认为这是一个类似的解决方法,手动设置"进度"偏移量.无论哪种情况,上述代码都适合您.


Arn*_*Rao 6

横向进度条自定义材质样式:

改变背景颜色和水平进度条的进度。

<style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:progressBackgroundTint">#69f0ae</item>
    <item name="android:progressTint">#b71c1c</item>
    <item name="android:minWidth">200dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

通过设置样式属性将其应用于进度条,对于自定义材质样式和自定义进度条检查http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples

  • 这需要最低 api 级别 21 (3认同)

Mar*_*hat 6

在 ProgressBar 中添加 Xml

对于 SDK ver 21 及更高版本

android:indeterminateTint="@color/red"
Run Code Online (Sandbox Code Playgroud)


Alé*_*lho 5

使用android.support.v4.graphics.drawable.DrawableCompat

            Drawable progressDrawable = progressBar.getIndeterminateDrawable();
            if (progressDrawable  != null) {
                Drawable mutateDrawable = progressDrawable.mutate();
                DrawableCompat.setTint(mutateDrawable, primaryColor);
                progressBar.setProgressDrawable(mutateDrawable);
            }
Run Code Online (Sandbox Code Playgroud)


Dev*_*jer 5

这对我有用。它也适用于较低版本。将此添加到您的syles.xml

<style name="ProgressBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在xml中像这样使用它

<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:theme="@style/ProgressBarTheme"
   />
Run Code Online (Sandbox Code Playgroud)


小智 5

如果有人想以编程方式更改颜色:

progressBar.progressTintList = ColorStateList.valueOf(Color.RED)
Run Code Online (Sandbox Code Playgroud)