2011-10-16 23 views
40

Używam ListView i dla każdego wiersza Mam row_item.xml i nadmuchać, że w kodzieJak zmienić domyślne obrazy CheckBox

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <CheckBox 
     android:id="@+id/chk" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/txtChoice" 
     android:textColor="#FF0000" 
     android:text="TEST" 
     android:layout_toRightOf="@id/chk" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

Jak zmienić pole wyboru użyć innego custom_1 obraz, gdy jest zaznaczone, a inny obraz niestandardowy_2, gdy nie jest zaznaczone?

Odpowiedz

137

rozciągliwej customdrawablecheckbox.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="false" android:drawable="@drawable/unchecked_drawable" /> 
    <item android:state_checked="true" android:drawable="@drawable/checked_drawable" /> 
    <item android:drawable="@drawable/unchecked_drawable" /> <!-- default state --> 
</selector> 

yourcheckbox xml:

<CheckBox 
    android:id="@+id/chk" 
    android:button="@drawable/customdrawablecheckbox" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+2

Dzięki! Działa doskonale dla mnie ... –

+0

jeśli chcę odznaczyć sprawdzony element, która właściwość selektora może być używana .. –

+0

Muszę programowo checkbox gen jak używać niestandardowego checkBox ktoś ma pomysł? – UmAnusorn

9

pole wyboru to przycisk, dzięki czemu można podać własny wyciąg z zaznaczenia odznacz stan i jako tło pola wyboru. Na przykład:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="false" android:drawable="@drawable/yourdrawable1" /> 
<item android:state_checked="true" android:drawable="@drawable/yourdrawable2" /> 
<item android:drawable="@drawable/yourdrawable1" /> <!-- default --> 
</selector> 

i umieść to w pliku file.xml w folderze do rysowania. W swojej wyboru:

<CheckBox 
    android:button="@drawable/file" 
    android:id="@+id/chk" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+1

Android: tło nie działa z obrazem przycisku, umieszcza inny obraz w widoku tła. –

+1

Ty r right ... – Blackbelt

+0

jeśli chcę odznaczyć zaznaczoną pozycję, której właściwości selektora można użyć .. –

4

Jego całkiem proste :) Najpierw trzeba utworzyć klasę, która CustomCheckBox rozszerzy CheckBox i zastąpi metodę onDraw(Canvas canvas):

public class CustomCheckBox extends CheckBox { 
private final Drawable buttonDrawable; 

public CustomCheckBox(Context context, AttributeSet set) { 
    super(context, set); 
    buttonDrawable = getResources().getDrawable(R.drawable.custom_check_box); 
    try { 
     setButtonDrawable(android.R.color.transparent); 
    } catch (Exception e) { 
     // DO NOTHING 
    } 
    setPadding(10, 5, 50, 5); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    buttonDrawable.setState(getDrawableState()); 

    final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; 
    final int height = buttonDrawable.getIntrinsicHeight(); 
    if (buttonDrawable != null) { 
     int y = 0; 

     switch (verticalGravity) { 
     case Gravity.BOTTOM: 
      y = getHeight() - height; 
      break; 
     case Gravity.CENTER_VERTICAL: 
      y = (getHeight() - height)/2; 
      break; 
     } 

     int buttonWidth = buttonDrawable.getIntrinsicWidth(); 
     int buttonLeft = getWidth() - buttonWidth - 5; 
     buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height); 
     buttonDrawable.draw(canvas); 
    } 
} 
} 

stworzyć także swój przełącznik nazwany custom_check_box w swojej rozciągliwej folderu:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:state_window_focused="false" 
     android:drawable="@drawable/btn_check_on" /> 
    <item android:state_checked="false" android:state_window_focused="false" 
     android:drawable="@drawable/btn_check_off" /> 
    <item android:state_checked="true" android:state_pressed="true" 
     android:drawable="@drawable/btn_check_on" />  
    <item android:state_checked="false" android:state_focused="true" 
     android:drawable="@drawable/btn_check_off" /> 
    <item android:state_checked="false" android:drawable="@drawable/btn_check_off" /> 
    <item android:state_checked="true" android:drawable="@drawable/btn_check_on" /> 
</selector> 

i używać niestandardowych ikon/IMG w XML powyżej dla wszystkich trzech stanach (focused/prasowanego/default)

używać składnika niestandardowego w XML tak:

<*package + class path*.CustomCheckBox // example com.mypackage.ui.CustomCheckBox if your project is named "mypackage" and the class is in the "ui" folder 
      android:text="@string/text" 
      android:checked="false" android:layout_width="fill_parent" 
      android:id="@+id/myCheckbox" android:layout_height="wrap_content"/> 

i Java:

private CustomCheckBox mCheckbox; 
mCheckbox = (CustomCheckBox) findviewbyid(R.id.myCheckbox); 

Działa, ponieważ użyłem go w obie strony :) A z kilkoma poprawkami działa również dla RadioButtons w ten sam sposób. Szczęśliwe kodowanie!

0

Możesz użyć selektora w xml, który jest używany do dynamicznego zmieniania obrazu pola wyboru zgodnie ze swoim sprawdzonym stanem.

Na przykład:

<item android:drawable="@drawable/ic_linkedin" android:state_checked="true" /> 
<item android:drawable="@drawable/ic_linkedin_disabled" android:state_checked="false" /> 

W poniższym pliku, jeśli pole jest zaznaczone, to będzie ustawić ikonę ic_linkedin a jeśli pole wyboru jest zaznaczone, to będzie ustawić ikonę ic_linkedin_disabled .

Powiązane problemy