我有一个现有的图层列表,用于自定义 ProgressBar,如下所示:
style_progress_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="#F6F3F1" />
<item android:id="@android:id/secondaryProgress">
<scale
android:drawable="#DE0012"
android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
<scale
android:drawable="#DE0012"
android:scaleWidth="100%" />
</item>
</layer-list>
此层列表用于覆盖 ProgressBar 的 progressDrawable 属性,并且像一个魅力一样工作。
现在,我想使用相同的主干,但我想以编程方式更改第二项和第三项的颜色,因为在很多情况下进度颜色应该会发生变化,并且我不会创建大量 xml 文件甚至不是一个好主意。
我设法阅读了上面的文件并阅读了它的第二个和第三个项目,但我找不到改变这两个项目的颜色的合适解决方案。除了颜色问题,一切都按预期工作。
LayerDrawable layers = (LayerDrawable) getResources().getDrawable(R.drawable.style_progress_bar);
int color = getResources().getColor(colorId); // This is the ID of the new color. I use it elsewhere so this should be 100% good
layers.getDrawable(1).setColorFilter(color, PorterDuff.Mode.MULTIPLY);
layers.getDrawable(2).setColorFilter(color, PorterDuff.Mode.MULTIPLY);
// I even called invalidate(); after all this. Nothing changed
我觉得我真的很接近结束这个,但我被卡住了。
有任何想法吗?谢谢!