2010-08-25 240 views
14

我無法自定義我的複選框,雖然我已經在xml首選項文件中定義了背景,但並未將其拉出。 1.我想顯示覆選框,自定義圖像和定義選擇XML作爲「android_button.xml」它看起來像:自定義複選框首選項

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checkable="true" 
     android:drawable="@drawable/state_normal" /> <!-- pressed --> 
<item android:state_checked="true" 
     android:drawable="@drawable/android_pressed" /> <!-- focused --> 
<item android:drawable="@drawable/state_normal" /> <!-- default --> 
</selector> 

state_normal和android_pressed在水庫2個巴紐圖像>繪製文件夾。

2.my複選框preference.xml文件是:

  <CheckBoxPreference android:key="@string/Drop_Option" 
      android:title="Close after call drop" 
      android:defaultValue="true" 
      android:background="@drawable/android_button" 
      /> 

是否有定義的任何錯誤,在屏幕上顯示出來的唯一變化是android:標題文字,如果我改變文本,它改變了文字。沒有其他變化。我該如何解決。謝謝你的建議。

回答

42

有兩種方法可以實現你所需要的,首先是定義在水庫自定義複選框佈局custom_chexbox.xml /佈局:

<?xml version="1.0" encoding="UTF-8"?> 
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/checkbox" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:focusable="false" 
android:clickable="false" android:button="@drawable/android_button"/> 

然後,你需要指定此佈局的偏好:

<CheckBoxPreference android:key="@string/Drop_Option" 
android:title="Close after call drop" android:defaultValue="true" 
android:widgetLayout="@layout/custom_checkbox"/> 

第二種方法是創建自定義主題,爲複選框視圖重新定義樣式並將主題應用於首選項活動,有關詳細信息,請參閱How to customize the color of the CheckMark color in android in a dialog. : android

+1

生病等級,對android:widgetLayout沒有任何線索,希望每個其他組件都有。 – Gubatron 2011-12-20 20:25:32

+0

夥計,這是完美的。 – zenperttu 2012-04-20 20:55:50

+0

這是完美的人。你是一個拯救生命的人。我從來不知道我們也可以使用自定義佈局的小部件。非常感謝。 – 2015-01-11 08:09:24

0

讓一個drwable XML文件:

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

    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_pressed="true"></item> 

</selector> 

設置它通過編程 cb.setButtonDrawable(R.drawable.checkboxcustom);