碰到动态改变 Drawble 的颜色的需要,记录一下
创建一个 shape 在 /drawble/t_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/darkBG"></solid>
<corners android:radius="4dp"/>
</shape>
然后需要动态改变 color
首先要获得 Drawble
Drawable tagShapeDrawable = Drawable.createFromXml(context.getResources(), context.getResources().getXml(R.drawable.tag_shape));
然后
tagShapeDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
view.setBackground(tagShapeDrawable);