我在我的Android应用程序中使用了一个水平进度条,并且我想改变它的进度颜色(默认情况下是黄色)。我如何使用code
(而不是XML)来做这件事?
发布于 2010-01-07 23:00:00
很抱歉,这不是答案,但是是什么驱动了从代码设置它的需求呢?如果定义正确,.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>
发布于 2012-06-07 17:14:24
对于我的不确定进度条(微调器),我只是在可绘制的上设置了一个滤色器。效果很好,只有一行。
将颜色设置为红色的示例:
ProgressBar spinner = new android.widget.ProgressBar(
context,
null,
android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
发布于 2019-09-25 13:39:25
这对我很有效。它也适用于低版本。将此代码添加到您的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"
/>
https://stackoverflow.com/questions/2020882
复制相似问题