Android - Tinting ProgressBar on pre-Lollipop devices

后端 未结 6 654
梦谈多话
梦谈多话 2020-12-31 05:27

My app\'s minimum API-level is 19 (KitKat), and contains a layout with a horizontal ProgressBar. I\'m using android:progressTint attribute to tint

6条回答
  •  情书的邮戳
    2020-12-31 05:29

    If you want to do it programmatically, try this

    public static void setSeekbarTint(SeekBar seekbar, int color) {
            PorterDuff.Mode porterDuffMode = PorterDuff.Mode.SRC_IN;
            if (seekbar.getIndeterminateDrawable() != null)
                seekbar.getIndeterminateDrawable().setColorFilter(color, porterDuffMode);
    
            if (seekbar.getProgressDrawable() != null &&
                    seekbar.getProgressDrawable() instanceof LayerDrawable) {
                LayerDrawable layerDrawable = (LayerDrawable) seekbar.getProgressDrawable();
                GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(android.R.id.background);
    
                layerDrawable.setColorFilter(color, porterDuffMode);
                seekbar.setProgressDrawable(layerDrawable);
                gradientDrawable.setColorFilter(Color.WHITE, porterDuffMode);
            } else if (seekbar.getProgressDrawable() != null &&
                    seekbar.getProgressDrawable() instanceof ClipDrawable) {
                ClipDrawable clipDrawable = (ClipDrawable) seekbar.getProgressDrawable();
                clipDrawable.setColorFilter(color, porterDuffMode);
                seekbar.setProgressDrawable(clipDrawable);
            }
    }
    

提交回复
热议问题