2017-01-02 12 views
0

私のdrawableのレイヤーの色をプログラム的に変更しようとしているので、実際のdrawable xmlファイルは変更されませんが、現在のdrawableはImageViewにsrcとして使用されています。Android drawableレイヤーの設定が失敗する

私が扱っているオブジェクトは、下に入れ子になっている3つの円(3つの楕円形)です(card_template.xml参照)。

私は、次のコードを持っている:私は理由System.out.println価値が、正しい描画可能になっていますが、私はlayer1.setColor(0);に層の色を設定すると、層がちょうど消える知っ

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); 

を - 外円は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をご覧ください。 0の代わりに適切なcolorstateオブジェクトを与えてみてください。

+0

これは、setColorにint引数があるため、答えには現実的ではありません。これはうまくいくはずです。 –

関連する問題