如何在Android中更改进度条的进度颜色

新手上路,请多包涵

我在我的 Android 应用程序中使用水平进度条,我想更改它的进度颜色(默认为黄色)。如何使用 code (不是 XML)来做到这一点?

原文由 WhiteTigerK 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 636
2 个回答

很抱歉,这不是答案,但是是什么推动了从代码中设置它的要求?如果定义正确, .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>

原文由 Alex Volovoy 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果有人想以编程方式更改颜色:

 progressBar.progressTintList = ColorStateList.valueOf(Color.RED)

原文由 Mohammed Alomair 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题