私の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>
これは、setColorにint引数があるため、答えには現実的ではありません。これはうまくいくはずです。 –