2017-01-02 7 views
0

드로어 블에서 레이어의 색상을 프로그래밍 방식으로 변경하려고하므로 실제 드로어 블 XML 파일은 변경되지 않지만 현재 ImageView에서 src로 사용되는 드로어 블이 변경됩니다.안드로이드 드로어 블 레이어 세트 컬러 실패

내가 작업하고있는 객체는 아래에 서로 중첩 된 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입니다 :) 도와주세요 : GradientDrawable 문서 setColor를에 따르면

<?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

ColorStateList합니다.

자세한 내용은 ColorStateList을 통해 확인하십시오. 0.

+0

int 형 인자를 가진 setColor가 있기 때문에 이것은 답이 없다. 그것도 잘 작동 할 것이고, 그것은 내가 사용하기를 원하는 것이다. –