我有点问题。搜索时发现你的帖子没有回复,所以我想和你分享我的解决方案。也许它可以帮助你。
我创建了一个可绘制的图层列表,并动态更改了其项目颜色。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item_bottom_stroke" >
<shape android:shape="rectangle">
<solid android:color="#0096FF"/>
</shape>
</item>
<item android:id="@+id/item_navbar_background" android:bottom="1dp" >
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"/>
</shape>
</item>
下面的代码在运行时修改上面的drawbable以更改其颜色。
LayerDrawable layerDrawable = (LayerDrawable) v.getContext().getResources().getDrawable(R.drawable.layer_list_navigation_with_border);
GradientDrawable strokeDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_bottom_stroke);
strokeDrawable.setColor(strokeColor[0]);
GradientDrawable backgroundColor = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_navbar_background);
backgroundColor.setColors(bgColor);