4

現在、/res/drawable フォルダーに次の xml ファイルがあります。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid
    android:color="@color/transparent_white" />

<stroke
    android:width="1dip"
    android:color="@color/light_gray" />
</shape>

縁取りしようとしているアイテムに応じて、背景を白、灰色、青などにしたい場合があります。唯一の違いがソリッド属性の色であるn個のxmlファイルを作成せずにこれを行う方法はありますか?

4

1 に答える 1

0

これは、形状を動的に宣言し、実行時に色を変更することで実現できます。

ShapeDrawable shapeDrawable= new ShapeDrawable();
shapeDrawable.setShape(new RectShape());
shapeDrawable.getPaint().setColor(<your color>);

((TextView) row.findViewById(<your viewid>)).setBackgroundDrawable(shapeDrawable);
于 2015-06-08T16:15:40.837 に答える