hhg*_*hhg 3 user-interface android layer-list
我有一个图层列表
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="@color/custom_color" />
</shape>
</item>
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@android:color/white"/>
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
并希望在我的项目中的多个位置重用此可绘制对象,以便@color/custom_color(在上面的示例中)在每种情况下都被替换为不同的颜色。应该有一种方法来实现它,而不是创建一个单独的可绘制对象。有任何想法吗?
小智 7
custom_color必须通过 访问矩形形状,id也就是说android:id="@+id/shape_rectangle"首先在 xml 中定义它:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shape_rectangle">
<shape android:shape="rectangle" >
<solid android:color="@color/custom_color" />
</shape>
</item>
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@android:color/white"/>
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
然后:
LayerDrawable shapeRectangle = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.custom_layer);
GradientDrawable gradient = (GradientDrawable) shapeRectangle.findDrawableByLayerId(R.id.shape_rectangle);
gradient.setColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)
替换custom_layer为您的绘图名称
| 归档时间: |
|
| 查看次数: |
5023 次 |
| 最近记录: |