我有一个 android 可绘制对象,我将把它应用到几个 TextView 的背景
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<item android:id="@+id/clr"
android:left="0.5dp" android:right="0.5dp" android:top="0.5dp" android:bottom="0.5dp" >
<shape android:shape="rectangle">
<solid android:color="#FFF000" />
</shape>
</item>
</layer-list>
我想以编程方式更改其颜色。我怎样才能做到这一点?
最佳答案
可以这样做
// "cellLabel" background colour of textview or button etc.
LayerDrawable layers = (LayerDrawable) cellLabel.getBackground();
// drawable item id
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.clr));
shape.setColor(my_color);
关于android - 更改 Android XML 中的形状颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20463082/