banner
RustyNail

RustyNail

coder. 【blog】https://rustynail.me 【nostr】wss://ts.relays.world/ wss://relays.world/nostr

TextView 动态改变Drawble的背景颜色

碰到动态改变 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);
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。