我正在使用Android应用程序中的水平进度条,并且想要更改其进度颜色(默认为黄色)。如何在代码
中完成此操作(不使用XML)?
我正在使用Android应用程序中的水平进度条,并且想要更改其进度颜色(默认为黄色)。如何在代码
中完成此操作(不使用XML)?
虽然这不是编程方法,但我认为它仍然可以帮助很多人。
我尝试了很多方法,最有效的方法是将以下行添加到我的.xml文件中的进度条:
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+
对于水平进度条,您也可以像这样使用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);
如果进度条是不确定的,则使用getIndeterminateDrawable()
而不是getProgressDrawable()
。
自 Lollipop(API 21)以来,您可以设置进度着色:
progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
getIndeterminateDrawable
做同样的操作。 - Thommy很抱歉这不是答案,但是是什么推动了从代码中设置的要求?.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>
对于我的不确定进度条(spinner),我只需在可绘制对象上设置颜色过滤器即可。这非常有效,只需要一行代码。
以下是将颜色设置为红色的示例:
ProgressBar spinner = new android.widget.ProgressBar(
context,
null,
android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
android.graphics.PorterDuff.Mode.MULTIPLY
更改为 PorterDuff.Mode.SRC_IN
,您将获得精确的十六进制颜色。 - micnguyen这是一个旧问题,但此处未提及使用主题。如果默认主题使用AppCompat
,则您定义的colorAccent
将成为ProgressBar
的颜色。
更改colorAccent
也将更改您的ProgressBar
的颜色,但这些更改也会反映在多个位置。因此,如果您只想针对特定的ProgressBar
设置不同的颜色,可以通过将主题应用于该ProgressBar
来实现:
扩展您的默认主题并覆盖colorAccent
<style name="AppTheme.WhiteAccent">
<item name="colorAccent">@color/white</item> <!-- Whatever color you want-->
</style>
在 ProgressBar
中添加 android:theme
属性:
android:theme="@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
的colorAccent
。
注意:使用style
将不起作用。您只需要使用android:theme
。
您可以在此处找到有关主题的更多用法:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH
全部API
如果使用全部API,只需要在style中创建主题。
style.xml
<resources>
//...
<style name="progressBarBlue" parent="@style/Theme.AppCompat">
<item name="colorAccent">@color/blue</item>
</style>
</resources>
并且正在使用中
<ProgressBar
...
android:theme="@style/progressBarBlue" />
API 级别 21 及更高版本
如果在 API 级别 21 或更高版本中使用,请使用以下代码:
<ProgressBar
//...
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/secondary"/>
这对我有效,而且较低版本也可用。将此添加到您的styles.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"
/>
parent="ThemeOverlay.AppCompat.Light"
- AdamHurwitz这对我很有效:
<ProgressBar
android:indeterminateTint="#d60909"
... />
根据一些建议,您可以指定一个形状和带颜色的 ClipDrawable,然后进行设置。我已经通过程序将其实现。以下是我的做法:
首先确保导入 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);
progress.setLevel(2500)
对我无效,显然你的编辑由于某些原因未被接受。谢谢。+1. - ateiob如果是未确定状态:
((ProgressBar)findViewById(R.id.progressBar))
.getIndeterminateDrawable()
.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
android:indeterminateTint="@android:color/white"
只在 API >=21 版本及以上有效。 - Madeyedexterandroid:progressTint="@android:color/white"
- Kamil