2014-06-18 1 views
2

프로그래밍 방식으로 드로어 블 선택기를 만들고 싶습니다. 모양은 다음 형식이어야합니다.선택기 드로잉 프로그래밍 방식으로

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"><shape> 
      <solid android:color="#4aa5d4" /> 
     </shape></item> 
    <item><shape> 
      <stroke android:width="1dp" android:color="#4aa5d4" /> 
     </shape></item> 

</selector> 

왜? 왜냐하면 그 2 색을 바꿀 수 있기를 바랍니다. 나는 이것을 위해 Drawable을 만들어야한다는 것을 알고 있습니다. 나는 이미이 같은 GradientDrawables 내 자신의 생성 관리 :

public GradientDrawable getBackgroundGradient() { 
    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] { BACKGROUND_GRADIENT_TOP_COLOR, BACKGROUND_GRADIENT_BOTTOM_COLOR }); 
    return gd; 
} 

을하지만 지금은 SelectorDrawable이 필요합니다.

+1

감사합니다. 나는 내가 그걸로 뭔가를 할 수 있다고 생각한다;) –

답변

4

좋아, 여기, 내가 링크 된 주제를 사용하여 무엇을 최대 온.

public StateListDrawable getSelectorDrawable(int color) { 
    StateListDrawable out = new StateListDrawable(); 
    out.addState(new int[] { android.R.attr.state_pressed }, createNormalDrawable(color)); 
    out.addState(StateSet.WILD_CARD, createStrokeDrawable(color)); 
    return out; 
} 

public GradientDrawable createNormalDrawable(int color) { 
    GradientDrawable out = new GradientDrawable(); 
    out.setColor(color); 
    return out; 
} 

public GradientDrawable createStrokeDrawable(int color) { 
    GradientDrawable out = new GradientDrawable(); 
    out.setStroke(1, color); 
    return out; 
} 
0

선택기 XML에서 자원으로 드로어 블을 제공 할 수 있습니다. 드로어 블 아무것도 (PNG, XML 등)이 같이

될 수 있습니다

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:drawable="@drawable/shape_highlighted" 
     android:state_pressed="true"/> 
    <item 
     android:drawable="@drawable/shape_disabled" 
     android:state_enabled="false"/> 
    <item 
     android:drawable="@drawable/shape_normal"/> 
</selector> 
+0

하지만 Drawable Ressource를 변경할 필요가있다. –

+0

특정 색상을 변경하려면 전체 선택기를 변경하십시오 (즉, 변경할 필요가있는 상태의 다른 드로어 블이있는 다른 선택기를 제공하십시오). Java를 사용하여 특정 상태 드로어 블을 변경할 수 있는지 확신하지 못합니다. – Booger

+0

모든 것을 해결했습니다. 내가 원하는 방식이었습니다. 내 대답보기;) –

관련 문제