2013-12-20 3 views
18

selectable_kachel_shape의 색상을 프로그래밍 방식으로 변경하려고합니다.프로그래밍 방식으로 레이어 목록 안의 도형에서 색상 변경

kachel_ticked_style.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 


    <item> 
     <shape 
      android:id="@+id/selectable_kachel_shape" 
      android:shape="rectangle" > 
      <stroke 
       android:width="5dp" 
       android:color="@color/headrbar_color" /> 
     </shape> 
    </item> 
    <item> 
     <rotate 
      android:fromDegrees="45" 
      android:pivotX="120%" 
      android:pivotY="100%" 
      android:toDegrees="45" > 
      <shape android:shape="line" > 
       <stroke 
        android:width="40dp" 
        android:color="@color/headrbar_color" /> 
      </shape> 
     </rotate> 
    </item> 
    <item 
     android:right="5dp" 
     android:top="5dp"> 
     <bitmap 
      android:gravity="top|right" 
      android:src="@drawable/selectable_tiles_check" /> 
    </item> 

</layer-list> 

내가 1.Why 내가 모양 NullPointerException이에 geting하고

LayerDrawable layers = (LayerDrawable) this.getActivity().getResources().getDrawable(R.drawable.kachel_ticked_style); 

GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape)); 
         shape.setColor(this.getActivity().getResources().getColor(android.R.color.background_dark); 

다음 코드에게 조각 안에 호출 오전 다음은 XML 파일입니다. setColor?

2. 프로그래밍 방식으로 레이어 목록 안에있는 도형 내부의 색을 어떻게 변경할 수 있습니까? 내가 답을 찾을 확인

답변

24

은, 난 그냥 모양에없는 항목 내부 kachel_ticked_style.xml 모양의 ID를 넣어했다 :

<item 
    android:id="@+id/selectable_kachel_shape"> 
     <shape 
      android:shape="rectangle" > 
      <stroke 
       android:width="5dp" 
       android:color="@color/headrbar_color" /> 
     </shape> 
    </item> 

을 그리고 당신은 색상 중 하나를 변경할 수 있습니다 shape.setColor 또는 shape.setStroke (경우 strokeWidth,은, strokeColor)를 호출 스트로크

+0

의 색상을 호출하는 형태 그러나이 같은 problem.This 나를 위해 –

+0

음 @ji을 작업을 나던 데 ncyabraham 당신은보기를 얻을 수있는 다음 drawable 다음 여기에있는 것처럼 gradiendrawable 모양을 얻을 [링크] (http://stackoverflow.com/questions/16636412/change-shape-solid-color-at-runtime-inside- drawable-xml-as-background) – M090009

+0

도형 객체를 획득하기 위해 수행 한 단계를 보여 주실 수 있습니까? 명확하지 않습니다. –