如何在Android中更改进度条的进度颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2020882/
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
How to change progress bar's progress color in Android
提问by WhiteTigerK
I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code
(not XML)?
我在我的 Android 应用程序中使用了一个水平进度条,我想更改它的进度颜色(默认为黄色)。我如何使用code
(而不是 XML)来做到这一点?
采纳答案by Alex Volovoy
I'm sorry that it's not the answer, but what's driving the requirement setting it from code ?
And .setProgressDrawable
should work if it's defined correctly
很抱歉这不是答案,但是是什么推动了从代码设置它的要求?.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>
回答by Torben Kohlmeier
For a horizontal ProgressBar, you can use a ColorFilter
, too, like this:
对于水平 ProgressBar,您也可以使用 a ColorFilter
,如下所示:
progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
Note:This modifies the appearance of all progress bars in your app. To only modify one specific progress bar, do this:
注意:这会修改应用中所有进度条的外观。要仅修改一个特定的进度条,请执行以下操作:
Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);
If progressBar is indeterminate then use getIndeterminateDrawable()
instead of getProgressDrawable()
.
如果 progressBar 是不确定的,则使用getIndeterminateDrawable()
而不是getProgressDrawable()
.
Since Lollipop (API 21) you can set a progress tint:
从 Lollipop (API 21) 开始,您可以设置进度色调:
progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
回答by FFirmenich
This is not programmatically but I think it could help a lot of people anyway.
I tried a lot and the most efficient way was to add this lines to my ProgressBar in the .xml File:
这不是以编程方式,但我认为无论如何它可以帮助很多人。
我尝试了很多,最有效的方法是将此行添加到 .xml 文件中的 ProgressBar 中:
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/secondary"
So in the end this code did it for me:
所以最后这段代码为我做了:
<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">
This solution works for API 21+
此解决方案适用于API 21+
回答by jhavatar
For my indeterminate progressbar (spinner) I just set a color filter on the drawable. Works great and just one line.
对于我不确定的进度条(微调器),我只是在可绘制对象上设置了一个滤色器。效果很好,只有一行。
Example where setting color to red:
将颜色设置为红色的示例:
ProgressBar spinner = new android.widget.ProgressBar(
context,
null,
android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
回答by kirtan403
This is an old question, but using theme is not mentioned here. If your default theme is using AppCompat
, your ProgressBar
's color will be colorAccent
you have defined.
这是一个老问题,但这里没有提到使用主题。如果您的默认主题正在使用AppCompat
,则您ProgressBar
的 颜色将是colorAccent
您定义的。
Changing colorAccent
will also change your ProgressBar
's color, but there changes also reflects at multiple places. So, if you want a different color just for a specific PregressBar
you can do that by applying theme to that ProgressBar
:
改变colorAccent
也会改变你ProgressBar
的颜色,但变化也会反映在多个地方。所以,如果你想要一个不同的颜色,PregressBar
你可以通过应用主题来做到这一点ProgressBar
:
Extend your default theme and override
colorAccent
<style name="AppTheme.WhiteAccent"> <item name="colorAccent">@color/white</item> <!-- Whatever color you want--> </style>
And in
ProgressBar
add theandroid:theme
attribute:android:theme="@style/AppTheme.WhiteAccent"
扩展您的默认主题并覆盖
colorAccent
<style name="AppTheme.WhiteAccent"> <item name="colorAccent">@color/white</item> <!-- Whatever color you want--> </style>
并
ProgressBar
添加android:theme
属性:android:theme="@style/AppTheme.WhiteAccent"
So it will look something like this:
所以它看起来像这样:
<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" />
So you are just changing a colorAccent
for your particular ProgressBar
.
所以你只是colorAccent
为你的特定ProgressBar
.
Note: Using style
will not work. You need to use android:theme
only.
You can find more use of theme here: https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH
注意:使用style
将不起作用。你android:theme
只需要使用。你可以在这里找到更多主题的使用:https: //plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH
回答by Rasoul Miri
All API
所有 API
if use all API just create the theme in style
如果使用所有 API 只需创建风格的主题
style.xml
样式文件
<resources>
//...
<style name="progressBarBlue" parent="@style/Theme.AppCompat">
<item name="colorAccent">@color/blue</item>
</style>
</resources>
and use in progress
并在使用中
<ProgressBar
...
android:theme="@style/progressBarBlue" />
API level 21 and higher
API 级别 21 及更高
if used in API level 21 and higher just use this code:
如果在 API 级别 21 及更高级别中使用,只需使用以下代码:
<ProgressBar
//...
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/secondary"/>
回答by PaulieG
as per some of the suggestions, you CAN specify a shape and clipdrawable with a colour, then set it. I have this working programatically. This is how I do it..
根据一些建议,您可以指定一个形状和带有颜色的可剪贴画,然后设置它。我有这个以编程方式工作。我就是这样做的..
First make sure you import the drawable library..
首先确保您导入可绘制库..
import android.graphics.drawable.*;
import android.graphics.drawable.*;
Then use the code similar to below;
然后使用类似于下面的代码;
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);
回答by MOH3N
This worked for me :
这对我有用:
<ProgressBar
android:indeterminateTint="#d60909"
... />
回答by the.knife
if Indeterminate:
如果不确定:
((ProgressBar)findViewById(R.id.progressBar))
.getIndeterminateDrawable()
.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
回答by Henrique de Sousa
Nowadays in 2016 I found some pre-Lollipop devices don't honour the colorAccent
setting, so my final solution for all APIs is now the following:
如今在 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);
}
For bonus points, it doesn't use any deprecated code. Try it!
对于奖励积分,它不使用任何弃用的代码。尝试一下!