2012-07-02 40 views
1

我需要有自定義的複選框,這不是那麼棘手。我做了checkbox_selector來繪製文件夾:如何爲所有元素設置相同的選擇器?

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" android:drawable="@drawable/checkbox_disabled" /><!-- disabled --> 
    <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/checkbox_unchecked_focused" /><!-- pressing unchecked box --> 
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/checkbox_checked_focused" /><!-- pressing checked box --> 
    <item android:state_checked="true" android:drawable="@drawable/checkbox_checked" /><!-- checked box --> 
    <item android:state_checked="false" android:drawable="@drawable/checkbox_unchecked" /><!-- unchecked box --> 

</selector> 

但問題是,每一個checkbutton我做,我已經添加

<Checkbox 
    android:button="@drawable/checkbox_selector" /> 

試圖谷歌答案了一段時間,也許我可以」找到合適的單詞。

我可以說在這個應用程序的每個複選框應該使用這個@ drawable/checkbox_selector嗎?

編輯: 說在styles.xml

<style name="customCheckBox"> 
    <item android:button>@drawable/checkbox_selector</item> 
</style> 

中的Android 2.3.3 爲我工作但當我嘗試過的Android 4.0.3這是行不通的。任何想法爲什麼是這樣?

回答

1

您可以使用樣式用於此目的: 在style.xml剛剛成立的:

<style name="CustomCheckBox" > 
    <item name="android:button">"@drawable/checkbox_selector</item> 
</style> 

然後就是應用你的主題,所有的複選框,這樣做:

<Checkbox style="@style/CustomCheckBox" /> 

欲瞭解更多關於款式的參考,請閱讀:

http://developer.android.com/guide/topics/ui/themes.html

+0

他會叫梃每一個複選框,同樣的問題,甚至是壞 –

+1

不,因爲我可以在主題文件說< item name =「android:checkboxStyle」> @ style/customCheckBox – joonasj

+0

您也可以在自定義複選框構造函數中設置樣式。 – dreamtale

1

你可以定義你對他們清單中的所有控制(主題包含了你的風格)

android:theme="@style/CustomCheckBox" 
相關問題