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 回答