遇到需要動態改變 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);