banner
RustyNail

RustyNail

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

TextViewのDrawableの背景色を動的に変更する

動的に Drawable の色を変更する必要がある場合は、次のように記録します。

/drawble/t_shape.xml に shape を作成します。

<?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>

次に、色を動的に変更する必要があります。

まず、Drawable を取得する必要があります。

Drawable tagShapeDrawable = Drawable.createFromXml(context.getResources(), context.getResources().getXml(R.drawable.tag_shape));

そして

tagShapeDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
view.setBackground(tagShapeDrawable);
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。