How to change progress bar's progress color in Android

前端 未结 30 2354
情话喂你
情话喂你 2020-11-22 07:49

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

30条回答
  •  不知归路
    2020-11-22 08:20

    simply use:

    PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        mode = PorterDuff.Mode.MULTIPLY;
    }
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
        progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(Color.RED));
    } else {
        Drawable progressDrawable;
        progressDrawable = (progressBar.isIndeterminate() ? progressBar.getIndeterminateDrawable() : progressBar.getProgressDrawable()).mutate();
        progressDrawable.setColorFilter(context.getResources().getColor(Color.RED), mode);
        progressBar.setProgressDrawable(progressDrawable);
    }
    

提交回复
热议问题