2017-01-02 88 views
0

我試圖以編程方式更改我的可繪製圖層的顏色,因此實際的可繪製xml文件不會更改,但僅用作我的ImageView中的src的當前可繪製對象。Android可繪製圖層設置顏色失敗

我正在使用的對象是下面相互嵌套的3個圓圈(3個橢圓形狀)(請參閱card_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);,該層就會消失 - 外圈不再顯示在keyCard ImageView中。

也不layer1.setColorFilter(0, PorterDuff.Mode.___)未在任何模式下工作時,無論層保持不變或消失..

這究竟是爲什麼?我無法在文檔中找到我的解決方案,也無法在此處或其他地方找到任何問題。

請幫助:)

這裏是card_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> 

回答

0

根據GradientDrawable文檔的setColor需要ColorStateList。

請通過ColorStateList瞭解更多。嘗試並給出一個適當的colorstate對象,而不是0.

+0

這是不是真正的答案,因爲也有一個int參數的setColor ...它應該工作,以及這就是我想要的:) –