2012-07-02 38 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" 
相关问题