我在我的Android应用程序中使用水平进度条,我想改变它的进度颜色(默认为黄色)。我如何使用代码(而不是XML)做到这一点?


很抱歉,这不是答案,但是是什么驱使需求从代码中设置它呢? 如果定义正确。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>

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

The name of the xml file must only contain characters: a-z0-9_. (ie. no capitals!) To reference your "drawable" it is R.drawable.filename To override the default look, you use myProgressBar.setProgressDrawable(...), however you need can't just refer to your custom layout as R.drawable.filename, you need to retrieve it as a Drawable:Resources res = getResources(); myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename); You need to set style before setting progress/secondary progress/max (setting it afterwards for me resulted in an 'empty' progress bar)

根据一些建议,你可以指定一个形状和可剪切的颜色,然后设置它。我让它以编程的方式工作。我就是这么做的。

首先确保导入了可绘制库。

进口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);
ProgressBar bar;

private Handler progressBarHandler = new Handler();

GradientDrawable progressGradientDrawable = new GradientDrawable(
        GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
                0xff1e90ff,0xff006ab6,0xff367ba8});
ClipDrawable progressClipDrawable = new ClipDrawable(
        progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] progressDrawables = {
        new ColorDrawable(0xffffffff),
        progressClipDrawable, progressClipDrawable};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);


int status = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
    setContentView(R.layout.startup);

    bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
    bar.setProgress(0);
    bar.setMax(100);

    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    bar.setProgressDrawable(progressLayerDrawable);
}

这帮助我通过代码设置自定义颜色为progressbar。希望能有所帮助

对于我的不确定的进度条(转轮),我只是设置了一个颜色过滤器的可绘制。工作很好,只是一行。

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

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

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);

发布了关于PaulieG的答案的补充信息,因为ateiob让我解释一些事情…


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

例如,如果progress = 45,而你试图将其设置为45,代码将什么都不做,也不会重绘进度。

调用ProgressBar.setProgressDrawable()后,你的进度条将是空白的(因为你改变了可绘制的部分)。

这意味着你需要设置进度,并重新绘制它。但是如果您只是将进度设置为保留值,它将什么也不做。

您必须先将其设置为0,然后再次设置为“旧”值,然后条将重新绘制。


总结一下:

保留“旧的”进度值 更新可绘制/颜色(使栏空白) 将进度重置为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();
}

至于HappyEngineer的解决方案,我认为它是一个类似的解决方案,手动设置“进度”偏移量。无论哪种情况,上面的代码都应该适用于您。

对于水平风格的ProgressBar,我使用:

import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;

public void setColours(ProgressBar progressBar,
                        int bgCol1, int bgCol2, 
                        int fg1Col1, int fg1Col2, int value1,
                        int fg2Col1, int fg2Col2, int value2)
  {
    //If solid colours are required for an element, then set
    //that elements Col1 param s the same as its Col2 param
    //(eg fg1Col1 == fg1Col2).

    //fgGradDirection and/or bgGradDirection could be parameters
    //if you require other gradient directions eg LEFT_RIGHT.

    GradientDrawable.Orientation fgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;
    GradientDrawable.Orientation bgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;

    //Background
    GradientDrawable bgGradDrawable = new GradientDrawable(
            bgGradDirection, new int[]{bgCol1, bgCol2});
    bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
    bgGradDrawable.setCornerRadius(5);
    ClipDrawable bgclip = new ClipDrawable(
            bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
    bgclip.setLevel(10000);

    //SecondaryProgress
    GradientDrawable fg2GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg2Col1, fg2Col2});
    fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg2GradDrawable.setCornerRadius(5);
    ClipDrawable fg2clip = new ClipDrawable(
            fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Progress
    GradientDrawable fg1GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg1Col1, fg1Col2});
    fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg1GradDrawable.setCornerRadius(5);
    ClipDrawable fg1clip = new ClipDrawable(
            fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Setup LayerDrawable and assign to progressBar
    Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
    LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    //Copy the existing ProgressDrawable bounds to the new one.
    Rect bounds = progressBar.getProgressDrawable().getBounds();
    progressBar.setProgressDrawable(progressLayerDrawable);     
    progressBar.getProgressDrawable().setBounds(bounds);

    // setProgress() ignores a change to the same value, so:
    if (value1 == 0)
        progressBar.setProgress(1);
    else
        progressBar.setProgress(0);
    progressBar.setProgress(value1);

    // setSecondaryProgress() ignores a change to the same value, so:
    if (value2 == 0)
        progressBar.setSecondaryProgress(1);
    else
        progressBar.setSecondaryProgress(0);
    progressBar.setSecondaryProgress(value2);

    //now force a redraw
    progressBar.invalidate();
  }

一个调用的例子是:

  setColours(myProgressBar, 
          0xff303030,   //bgCol1  grey 
          0xff909090,   //bgCol2  lighter grey 
          0xff0000FF,   //fg1Col1 blue 
          0xffFFFFFF,   //fg1Col2 white
          50,           //value1
          0xffFF0000,   //fg2Col1 red 
          0xffFFFFFF,   //fg2Col2 white
          75);          //value2

如果您不需要“次要进度”,只需将value2设置为value1。

对于水平的ProgressBar,你也可以使用ColorFilter,就像这样:

progressBar.getProgressDrawable().setColorFilter(
    Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

注意:这将修改应用程序中所有进度条的外观。要只修改一个特定的进度条,请执行以下操作:

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

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

因为棒棒糖(API 21),你可以设置一个进度色调:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

如果不确定的:

((ProgressBar)findViewById(R.id.progressBar))
    .getIndeterminateDrawable()
    .setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

这不是程序化的,但我认为它可以帮助很多人。 我尝试了很多,最有效的方法是在。xml文件中添加这行到我的ProgressBar:

            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary"

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

<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">

该解决方案适用于API 21+

将此自定义样式应用于进度条。

<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
        <item name="android:indeterminateDrawable">@drawable/progress</item>
        <item name="android:duration">40</item>
        <item name="android:animationCache">true</item>
        <item name="android:drawingCacheQuality">low</item>
        <item name="android:persistentDrawingCache">animation</item>
    </style>

@drawable / progress.xml -

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white"
    android:pivotX="50%"
    android:pivotY="50%" />

使用此类型的图像作为进度条。

为了获得更好的效果,您可以使用多个进度图像。请不要犹豫使用图像,因为Android平台本身使用图像作为进度条。 代码是从sdk中提取的:)

我是如何在水平ProgressBar中做到的:

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

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

如果您的主题是从theme继承的。AppCompat, ProgressBar将假定你在主题中定义为“colorAccent”的颜色。

所以,使用. .

< name = " colorAccent > @color /项目custom_color < - >项目

..将自动将ProgressBar的颜色着色为@color/custom_color。

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

改变colorAccent也会改变你的ProgressBar的颜色,但这些变化也反映在多个地方。所以,如果你想为一个特定的ProgressBar使用不同的颜色,你可以通过应用theme到那个ProgressBar来实现:

扩展默认主题并覆盖colorAccent <样式名= " AppTheme。WhiteAccent”> <item name="colorAccent">@color/white</item> <!——你想要什么颜色都行——> > < /风格 在ProgressBar中添加android:theme属性: android:主题= " @style / AppTheme。WhiteAccent”

所以它看起来是这样的:

<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" />

你只是在为你特定的ProgressBar改变一个颜色重音。

注意:使用样式将不起作用。你只需要使用android:theme。 你可以在这里找到更多关于theme的用法:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

如今在2016年,我发现一些前棒棒糖设备不尊重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);
}

另外,它没有使用任何弃用代码。试一试!

android:progressTint="#ffffff" 

适用于SDK 21及以上版本

android:indeterminateTint="@color/orange"

对我来说是很简单的。

水平ProgressBar使用矩形形状绘制背景,ClipDrawable从矩形形状构建的进展(主要和次要)。着色将颜色改变为某种色调。如果你想分别为这三种颜色设置目标颜色,你可以使用ProgressBar.setProgressDrawable(),如下所示:

    LayerDrawable progressBarDrawable = new LayerDrawable(
        new Drawable[]{
                new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                            new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),

                new ClipDrawable(
                            new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                    new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                            Gravity.START,
                            ClipDrawable.HORIZONTAL),

                new ClipDrawable(
                        new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                        Gravity.START,
                        ClipDrawable.HORIZONTAL)
            });

    progressBarDrawable.setId(0,android.R.id.background);
    progressBarDrawable.setId(1,android.R.id.secondaryProgress);
    progressBarDrawable.setId(2,android.R.id.progress);
    ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);

注意,在初始化LayerDrawable时,可绘制层的顺序很重要。首先绘制的应该是背景。根据我的实验,切换id是行不通的。如果你将填充设置为progressbar,那么这种方法将不起作用。如果你需要填充,那么你可以使用一个容器的进度条,如线性布局。

还有一件小事,如果你继承了一个基础主题,主题解决方案也会起作用,所以对于应用紧凑,你的主题应该是:

<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/custom</item>
</style>

然后在进度条主题中设置这个

<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"/>

使用attr非常简单,如果你正在处理多风格的应用程序:

试试这个方法:

在属性attrs.xml下面声明

 <attr name="circularProgressTheme" format="reference"></attr>

将下面的代码粘贴到styles.xml中

 <style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorAccent">#FF0000</item>
    </style>

    <style name="circularProgressThemeWhite">
        <item name="android:theme">@style/ProgressThemeWhite</item>
    </style>


  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

   <item name="circularProgressTheme">@style/circularProgressThemeWhite</item>

 </style>

使用进度条如下所示

  <ProgressBar
        style="?attr/circularProgressTheme"
        android:id="@+id/commonProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="visible"/>

这招对我很管用:

<ProgressBar
 android:indeterminateTint="#d60909"
 ... />

下面是通过编程方式更改ProgressBar颜色的代码。

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));

这个解决方案对我很有效:

<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"/>

水平进度条自定义材质样式:

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

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

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

最简单的解决方案,如果你想改变布局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" />

你可以尝试改变你的风格,主题,或使用android:indeterminateTint="@color/yourColor"任何你想要的地方,但只有一种方法来做,将工作在任何android SKD版本:

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

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );

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

progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );

Android真是一团糟!

这就是我所做的。工作。

ProgressBar:

<ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:indeterminateDrawable="@drawable/progressdrawable"
           />

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>

示例:

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

            Drawable progressDrawable = progressBar.getIndeterminateDrawable();
            if (progressDrawable  != null) {
                Drawable mutateDrawable = progressDrawable.mutate();
                DrawableCompat.setTint(mutateDrawable, primaryColor);
                progressBar.setProgressDrawable(mutateDrawable);
            }
ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);

freeRamPb.getProgressDrawable().setColorFilter(
Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);

所有接口

如果使用所有的API只创建主题的样式

style.xml

<resources>

    //...

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

</resources>

在进行中使用

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />

空气污染指数21或以上

如果在API级别21及以上使用,请使用以下代码:

<ProgressBar
   //...
   android:indeterminate="true"
   android:indeterminateTintMode="src_atop"
   android:indeterminateTint="@color/secondary"/>

更改水平进度条颜色(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)
    }
}

更改不确定的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)
            }
        }
    }
}

它最终正常地着色pre-lollipop progressBars

简单的使用方法:

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

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

<style name="ProgressBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/colorPrimary</item>
</style>

在xml中像这样使用它

<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:theme="@style/ProgressBarTheme"
   />

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

<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"
                        />

只需添加

                        android:progressTint="#82e9de" //for foreground colour
                        android:progressBackgroundTint="#82e9de" //for background colour

简单的使用方法:

DrawableCompat.setTint(progressBar.getIndeterminateDrawable(),yourColor)

因为方法公共无效setColorFilter(@ColorInt int颜色,@NonNull PorterDuff。模式模式)已弃用,我使用这样的形式:

progressBar.indeterminateDrawable.colorFilter =
            PorterDuffColorFilter(ContextCompat.getColor(this, R.color.black), PorterDuff.Mode.SRC_IN)

在新版本的材料设计库中,一个不确定的线性进度指示器似乎可以被制作成多种颜色。

使进度条的动画类型连续可以实现这一点。

在具有此属性的布局中: 应用:indeterminateAnimationType 或者用这个方法编程: setIndeterminateAnimationType ()

更多信息请参考这里。

在Xml中添加ProgressBar

适用于SDK 21及以上版本

android:indeterminateTint="@color/red"

我知道我迟到了,但如果你只想改变进度条的颜色,这是最简单的解决方案,在你的主题中,只需添加,

@color - your_color

colorSecondary将改变你整个进程的颜色。

默认值(不确定)

add

android:indeterminateTint="@color/white"

为确定

    android:progressTint="@color/color_1"

    //OR
    progressBar.getProgressDrawable().setColorFilter( PorterDuffColorFilter(Color.RED,android.graphics.PorterDuff.Mode.SRC_IN));

相信我,最简单的解决方法就是把这个粘贴到progressBar中:

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

如果有人想通过编程改变颜色:

progressBar.progressTintList = ColorStateList.valueOf(Color.RED)