2013-04-17 40 views
0

當我從xml中更改複選框的按鈕時,複選框不會反映已選中的選項。 我的XML文件是:複選框未選中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

<TextView 
    android:id="@+id/memberName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="57.56" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#000000"/> 

<CheckBox 
    android:id="@+id/checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:button="@drawable/checkbox" <----- change button of checkbox 
    /> 

@繪製/複選框png文件

+3

是什麼'@繪製/ checkbox'?是一個PNG文件或XML選擇器? IT應該是後者的條目,用於選中和取消選中。如果它發佈它的代碼。 – FoamyGuy

+0

@ drawable /複選框?是PNG文件 –

回答

1

爲了工作android:button需要被設置爲它定義了兩個檢查可繪製一個選擇的XML文件和未經檢查的國家。嘗試將其設置是這樣的:

checkbox_selector.xml

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

其中checkbox_checked.png是要在被選中的框中使用的圖像。 checkbox_unchecked.png是未檢查時的圖像。

然後在你的主要佈局這樣設置按鈕:

<CheckBox 
android:id="@+id/checkbox" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:button="@drawable/checkbox_selector" 
/> 
+0

Thanx FoamyGuy。 –