【发布时间】:2011-02-07 22:50:29
【问题描述】:
我想知道如何将不确定的ProgressBar 颜色从基础白色/灰色更改为黑色?当我更改indeterminateDrawable 时,我得到的是静态图像而不是移动的动画进度条。有没有办法简单地在 XML 中做到这一点?
【问题讨论】:
标签: android android-progressbar
我想知道如何将不确定的ProgressBar 颜色从基础白色/灰色更改为黑色?当我更改indeterminateDrawable 时,我得到的是静态图像而不是移动的动画进度条。有没有办法简单地在 XML 中做到这一点?
【问题讨论】:
标签: android android-progressbar
通过材质组件库,您可以使用具有以下属性的 CircularProgressIndicator:
indicatorColortrackColor类似:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:indeterminate="true"
app:trackColor="@color/red600Light"
app:indicatorColor="@color/red600Dark"
app:indicatorSize="50dp"
.../>
您也可以使用多色不确定指示器。
<com.google.android.material.progressindicator.CircularProgressIndicator
android:indeterminate="true"
app:trackColor="@color/red600Light"
app:indicatorColor="@array/progress_colors"
app:indeterminateAnimationType="contiguous"/>
array/progress_colors:
<integer-array name="progress_colors">
<item>@color/...</item>
<item>@color/...</item>
<item>@color/...</item>
</integer-array>
【讨论】:
在您的 AppTheme 中覆盖 android:colorControlActivated,它应该在您的 styles.xml 中:
<style name="AppTheme" parent="...">
...
<item name="android:colorControlActivated">@color/beautiful_color</item>
...
</style>
适用于 API 21+
【讨论】:
对于少于 23 的 API 使用
progressBar.getIndeterminateDrawable().setColorFilter(
getResources().getColor(Your_Color),
android.graphics.PorterDuff.Mode.SRC_IN);
别用
progressBar.getIndeterminateDrawable().setColorFilter(
ContextCompat.getColor(context, Your_Color),
android.graphics.PorterDuff.Mode.SRC_IN);
【讨论】:
我看到其他答案已经很老了,为了更改不确定的 ProgressBar 颜色,您只需在 XML 中直接在 ProgressBar 项目中设置 android:indeterminateTint 和 android:indeterminateTintMode 属性:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/colorPrimary"
android:indeterminateTintMode="src_in"
android:indeterminate="true" />
android:indeterminateTint - 应用于不确定进度指示器的色调。
必须是颜色值,形式为
"#rgb"、"#argb"、"#rrggbb"、"#aarrggbb",或对资源@color/colorPrimary的引用。
android:indeterminateTintMode - 用于应用进度指示器色调的混合模式。
必须是以下常量值之一:
add、multiply、screen、src_atop、src_in或src_over
这些属性的 Getter 和 Setter 方法是:
getIndeterminateTintList()getIndeterminateTintMode()setIndeterminateTintList(ColorStateList tint)setIndeterminateTintMode(PorterDuff.Mode tintMode)全部添加到API level 21
【讨论】:
progressBar.getIndeterminateDrawable().setColorFilter(
getResources().getColor(Your_Color),
android.graphics.PorterDuff.Mode.SRC_IN);
并将 Your_Color 替换为所需的颜色,例如:R.color.your_color_code
【讨论】:
android:indeterminateTint="@colors/your_color"android:indeterminateTintMode="src_in"
<?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>
【讨论】:
实际上你需要做的(对于圆和条)是在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 你可能需要调整进度条的大小以根据需要显示
【讨论】:
【讨论】:
要在要在白色/浅色背景上使用的默认主题中获取 ProgressBar,请使用其中一种反向样式:
<ProgressBar style="@android:style/Widget.ProgressBar.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Large.Inverse"/>
<ProgressBar style="@android:style/Widget.ProgressBar.Small.Inverse"/>
这通常会在透明的 ProgressBar 上显示为黑色,但某些操作系统安装使用自定义资源。如果您正在寻找特定颜色,则必须按照 CommonsWare 提供的说明滚动您自己的可绘制对象。
【讨论】: