如何更改android不确定的ProgressBar颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2638161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:42:11  来源:igfitidea点击:

How to change android indeterminate ProgressBar color?

androidandroid-progressbar

提问by mwa

I would like to know how I can change indeterminate ProgressBarcolor from basis white/grey color to black ? When I change the indeterminateDrawable, I get a static image instead of a moving animated progressBar. Is there any way to do it simply in XML?

我想知道如何将不确定的ProgressBar颜色从基础白色/灰色更改为黑色?当我更改 时indeterminateDrawable,我得到一个静态图像而不是一个移动的动画进度条。有什么办法可以简单地在 XML 中做到这一点吗?

回答by Zeyad Assem

progressBar.getIndeterminateDrawable().setColorFilter(
            getResources().getColor(Your_Color),
            android.graphics.PorterDuff.Mode.SRC_IN);

and replace Your_Color with the desired color like: R.color.your_color_code

并将 Your_Color 替换为所需的颜色,例如:R.color.your_color_code

回答by Anm

To get a ProgressBar in the default theme that is to be used on white/light back ground, use one of the inverse styles:

要在要在白色/浅色背景上使用的默认主题中获取 ProgressBar,请使用其中一种反向样式:

<ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Large.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Small.Inverse"/>

This will usually give you a black on transparent ProgressBar, but some OS installs use custom assets. If you're looking for a specific color, you'll have to roll your own drawables by following the instructions provided by CommonsWare.

这通常会在透明的 ProgressBar 上显示黑色,但某些操作系统安装使用自定义资产。如果您正在寻找特定颜色,则必须按照 CommonsWare 提供的说明滚动自己的可绘制对象。

回答by Cliff Burton

I see other answers are quite old, in order to change the indeterminate ProgressBar color you have just to set android:indeterminateTintand android:indeterminateTintModeattributes in your ProgressBaritem directly in XML:

我看到其他的答案是很老,为了改变你刚才要设置不确定的进度条的颜色android:indeterminateTintandroid:indeterminateTintMode你的属性ProgressBar直接在XML项:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateTint="@color/colorPrimary"
    android:indeterminateTintMode="src_in"
    android:indeterminate="true" />

android:indeterminateTint- Tint to apply to the indeterminate progress indicator.

android:indeterminateTint- 应用于不确定进度指示器的色调。

Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", "#aarrggbb", or a reference to a resource @color/colorPrimary.

必须是一个颜色值,在形式"#rgb""#argb""#rrggbb""#aarrggbb",或以一个资源引用@color/colorPrimary

android:indeterminateTintMode- Blending mode used to apply the progress indicator tint.

android:indeterminateTintMode- 用于应用进度指示器色调的混合模式。

Must be one of the following constant values:
add, multiply, screen, src_atop, src_inor src_over

必须是以下常量之一:
addmultiplyscreensrc_atopsrc_in或者src_over

Getter and Setter methods for these attributes are:

这些属性的 Getter 和 Setter 方法是:

All of them were added in API level 21

所有这些都是在API 级别 21中添加的

回答by ihrupin

回答by andro_stackoverflow

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@drawable/bg" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/secondary" />
    <item android:id="@android:id/progress" android:drawable="@drawable/progress" />
</layer-list>

回答by yasirbhat564

For API less than 23 use

对于小于 23 的 API 使用

progressBar.getIndeterminateDrawable().setColorFilter(
       getResources().getColor(Your_Color),
        android.graphics.PorterDuff.Mode.SRC_IN);

else use

否则使用

progressBar.getIndeterminateDrawable().setColorFilter(
        ContextCompat.getColor(context, Your_Color),
        android.graphics.PorterDuff.Mode.SRC_IN);

回答by monkey

Override android:colorControlActivatedin your AppTheme which should be in your styles.xml:

android:colorControlActivated在你的 AppTheme 中重写,它应该在你的 style.xml 中:

<style name="AppTheme" parent="...">
    ...
    <item name="android:colorControlActivated">@color/beautiful_color</item>
    ...
</style>

Works on API 21+

适用于 API 21+

回答by littlekitten

You may use the attribute "android:indeterminateTint" starting from API Level 21

您可以从 API 级别 21 开始使用属性“android:indeterminateTint”

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressb"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:indeterminateTint="@color/colorPrimary"
    android:progress="60"
    />

回答by lawnile

actually all u need to do (for both cirle and bar) is create an xml file in drawable as shown below...... progress_spinner_001 points to whatever image u want to animate and duration ... how long u want to display each frame for....... and set ur android:indeterminateDrawable= filename_in_drawable....

实际上所有你需要做的(对于 cirle 和 bar)就是在 drawable 中创建一个 xml 文件,如下所示...... progress_spinner_001 指向你想要动画和持续时间的任何图像......你想要显示每个图像多长时间框架......并设置你的 android:indeterminateDrawable= filename_in_drawable....

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
<item android:drawable="@drawable/progress_spinner_001" android:duration="300" />
<item android:drawable="@drawable/progress_spinner_002" android:duration="300" />
<item android:drawable="@drawable/progress_spinner_003" android:duration="300" />
  <item android:drawable="@drawable/progress_spinner_004" android:duration="300" />
    <item android:drawable="@drawable/progress_spinner_005" android:duration="300" />
      <item android:drawable="@drawable/progress_spinner_006" android:duration="300" />
     <item android:drawable="@drawable/progress_spinner_007" android:duration="300" />
    <item android:drawable="@drawable/progress_spinner_008" android:duration="300" />
</animation-list>

ps u may need to resize the progressbar to show as desired

ps 你可能需要调整进度条的大小以根据需要显示