Android layout 使用多种背景色重用边框资源

Android layout 使用多种背景色重用边框资源,android-layout,android-shape,Android Layout,Android Shape,我当前在/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

我当前在/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文件的情况下执行此操作,其中唯一的区别是实体属性的颜色?

您可以通过动态声明形状并在运行时更改颜色来执行此操作

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

((TextView) row.findViewById(<your viewid>)).setBackgroundDrawable(shapeDrawable);
ShapeDrawable ShapeDrawable=新的ShapeDrawable();
setShape(新的RectShape());
shapeDrawable.getPaint().setColor();
((TextView)row.findViewById()).setBackgroundDrawable(shapeDrawable);

我理解你的意思,但我正在寻找一种使用
XML
而不是
Java
的解决方案,因为它非常简单。我想要XML解决方案,因为它使编码更容易。