如何在Android中更改进度条的进度颜色 [英] How to change progress bar's progress color in Android

查看:51
本文介绍了如何在Android中更改进度条的进度颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)?

推荐答案

对于水平 ProgressBar,您也可以使用 ColorFilter,如下所示:

For a horizontal ProgressBar, you can use a ColorFilter, too, like this:

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);

如果 progressBar 不确定,则使用 getIndeterminateDrawable() 而不是 getProgressDrawable().

If progressBar is indeterminate then use getIndeterminateDrawable() instead of getProgressDrawable().

从 Lollipop (API 21) 开始,您可以设置进度色调:

Since Lollipop (API 21) you can set a progress tint:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

这篇关于如何在Android中更改进度条的进度颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆