Android可绘制图层集颜色失败

Android可绘制图层集颜色失败,android,xml,android-layout,android-drawable,xml-drawable,Android,Xml,Android Layout,Android Drawable,Xml Drawable,我试图以编程方式更改drawable中某个层的颜色,因此实际的drawable xml文件不会更改,而只更改在我的ImageView中用作src的当前drawable 我正在处理的对象是一个相互嵌套的3个圆(3个椭圆形)(请参见下面的card\u template.xml) 我有以下代码: LayerDrawable cardLayers = (LayerDrawable) keyCard.getDrawable(); System.out.println("Number of

我试图以编程方式更改drawable中某个层的颜色,因此实际的drawable xml文件不会更改,而只更改在我的
ImageView
中用作src的当前drawable

我正在处理的对象是一个相互嵌套的3个圆(3个椭圆形)(请参见下面的
card\u template.xml

我有以下代码:

    LayerDrawable cardLayers = (LayerDrawable) keyCard.getDrawable();
    System.out.println("Number of layers (should be 3): "+ cardLayers.getNumberOfLayers());
    GradientDrawable layer1 = (GradientDrawable) (cardLayers.findDrawableByLayerId(R.id.card_layer1));
    layer1.setColor(0);
由于
System.out.println
值,我知道我得到了正确的可绘制图形,但是当我在
layer1.setColor(0)中设置图层的颜色时
,该层刚刚消失-外圈不再显示在
钥匙卡
图像视图中

layer1.setColorFilter(0,PorterDuff.Mode.\uuuuu)
在任何模式下都不工作,层要么保持不变,要么消失

为什么会这样?我找不到我的解决方案,不是在文档中,也不是在这里或其他地方的任何问题中

请帮忙:)

下面是
card\u template.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/card_layer1">
    <shape android:shape="oval">
        <solid android:color="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <size
            android:width="5dp"
            android:height="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </shape>
</item>
<item android:id="@+id/card_layer2" android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp">
    <shape android:shape="oval">
        <solid android:color="#ff0000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <size
            android:width="5dp"
            android:height="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </shape>
</item>

<item android:id="@+id/card_layer3" android:top="4dp" android:left="4dp" android:right="4dp" android:bottom="4dp">
    <shape android:shape="oval">
        <solid android:color="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <size
            android:width="5dp"
            android:height="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </shape>
</item>

根据文档设置颜色采用颜色状态列表


请通过阅读了解更多信息。尝试给出一个合适的colorstate对象,而不是0。

这与答案无关,因为还有一个setColor with int参数。。。它应该也能工作,这就是我想要使用的:)