pea*_*mak 6 android colors android-progressbar layerdrawable
我想以编程方式将颜色设置为进度条primaryProgress,secondaryProgress,因为颜色将根据屏幕的背景颜色进行更改.
LayerDrawable progressDrawable = (LayerDrawable) ProgressBar1.getProgressDrawable();
Drawable backgroundColor = progressDrawable.getDrawable(0);
Drawable secondaryColor = progressDrawable.getDrawable(1);
Drawable primaryColor = progressDrawable.getDrawable(2);
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
primaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
secondaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
primaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);
secondaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);
progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
...
Run Code Online (Sandbox Code Playgroud)
**此处的颜色代码仅供测试.之后,颜色代码将被引用到其他部分以进行相应的更新
secondaryColor.setColorFilter((Color.rgb(255, 0, 0)), PorterDuff.Mode.SRC_OVER);
primaryColor.setColorFilter((Color.rgb(0, 255, 213)), PorterDuff.Mode.SRC_OVER);
progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
progressDrawable.setDrawableByLayerId(progressDrawable.getId(1), new ClipDrawable(secondaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
ProgressBar1.setProgressDrawable(progressDrawable);
ProgressBar1.setProgress(progress);
ProgressBar1.setSecondaryProgress(secondaryProgress);
Run Code Online (Sandbox Code Playgroud)
它强调红色primanyColor.setColor并报告The method setColor(int, null) is undefined for the type Drawable.
我怎么能修改上面的代码才能使它有效?谢谢!
以下是针对 min sdk 版本 21 及更高版本以编程方式更改 ProgressBar 颜色的代码
ProgressBar myProgress = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
myProgress.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
Run Code Online (Sandbox Code Playgroud)
要设置 Drawable 的颜色,请使用Drawable.setColorFilter。喜欢:
primaryColor.setColorFilter((Color.rgb(0,128,0)),
PorterDuff.Mode.SRC_OVER);
Run Code Online (Sandbox Code Playgroud)