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


当前回答

这是一个老问题,但这里没有提到使用主题。如果你的默认主题是使用AppCompat,你的ProgressBar的颜色将是你已经定义的colorAccent。

改变colorAccent也会改变你的ProgressBar的颜色,但这些变化也反映在多个地方。所以,如果你想为一个特定的ProgressBar使用不同的颜色,你可以通过应用theme到那个ProgressBar来实现:

扩展默认主题并覆盖colorAccent <样式名= " AppTheme。WhiteAccent”> <item name="colorAccent">@color/white</item> <!——你想要什么颜色都行——> > < /风格 在ProgressBar中添加android:theme属性: android:主题= " @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改变一个颜色重音。

注意:使用样式将不起作用。你只需要使用android:theme。 你可以在这里找到更多关于theme的用法:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH

其他回答

ProgressBar bar;

private Handler progressBarHandler = new Handler();

GradientDrawable progressGradientDrawable = new GradientDrawable(
        GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
                0xff1e90ff,0xff006ab6,0xff367ba8});
ClipDrawable progressClipDrawable = new ClipDrawable(
        progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] progressDrawables = {
        new ColorDrawable(0xffffffff),
        progressClipDrawable, progressClipDrawable};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);


int status = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
    setContentView(R.layout.startup);

    bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
    bar.setProgress(0);
    bar.setMax(100);

    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    bar.setProgressDrawable(progressLayerDrawable);
}

这帮助我通过代码设置自定义颜色为progressbar。希望能有所帮助

这个答案中可能有一件事没有提到:

如果您的主题是从theme继承的。AppCompat, ProgressBar将假定你在主题中定义为“colorAccent”的颜色。

所以,使用. .

< name = " colorAccent > @color /项目custom_color < - >项目

..将自动将ProgressBar的颜色着色为@color/custom_color。

因为方法公共无效setColorFilter(@ColorInt int颜色,@NonNull PorterDuff。模式模式)已弃用,我使用这样的形式:

progressBar.indeterminateDrawable.colorFilter =
            PorterDuffColorFilter(ContextCompat.getColor(this, R.color.black), PorterDuff.Mode.SRC_IN)

下面是通过编程方式更改ProgressBar颜色的代码。

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));

对于我的不确定的进度条(转轮),我只是设置了一个颜色过滤器的可绘制。工作很好,只是一行。

将颜色设置为红色的示例:

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);