若对系统自带的widget外观不满意,可以进行定制,原理是修改widget属性对应的drawable,操作步骤如下:
1.在android系统的styles.xml/theme.xml中找到控件的属性所对应的drawable(图片或selector);
2.在项目的styles.xml中自定义风格,继承系统风格,修改相应的drawable;
3.在layout.xml中设置widget的style = "@style/自定义style";
CheckBox定制
- 在sdk/platforms/android-**/data/res文件夹内搜索"styles.xml",并打开;
- 找到所需控件的style:
<style name="Widget.CompoundButton.CheckBox"> <item name="android:button">?android:attr/listChoiceIndicatorMultiple</item> </style>
属性开头是"?",表明引用了系统的theme属性; - 继续在当前目录内搜索"theme.xml",打开后找到"listChoiceIndicatorMultiple":
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>
- 再次搜索"btn_check",可以找到"btn_check.xml":
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Enabled states --> <item android:state_checked="true" android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_check_on" /> <item android:state_checked="false" android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_check_off" /> <item android:state_checked="true" android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/btn_check_on_pressed" /> <item android:state_checked="false" android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/btn_check_off_pressed" /> <item android:state_checked="true" android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_check_on_selected" /> <item android:state_checked="false" android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_check_off_selected" /> <item android:state_checked="false" android:state_enabled="true" android:drawable="@drawable/btn_check_off" /> <item android:state_checked="true" android:state_enabled="true" android:drawable="@drawable/btn_check_on" /> <!-- Disabled states --> <item android:state_checked="true" android:state_window_focused="false" android:drawable="@drawable/btn_check_on_disable" /> <item android:state_checked="false" android:state_window_focused="false" android:drawable="@drawable/btn_check_off_disable" /> <item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/btn_check_on_disable_focused" /> <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/btn_check_off_disable_focused" /> <item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" /> <item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" /> </selector>
可知系统定义了该widget选中/未选中时的图片,因此定制时,通过创建style,继承系统的checkbox风格,引用自定义selector即可; - 在项目drawable目录下创建"selector_my_checkbox.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/my_checkbox_selected" ></item> <item android:drawable="@drawable/my_checkbox_unselected"></item> </selector>
- 在项目values/styles.xml中自定义style:
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/selector_my_checkbox</item> </style>
- 设置CheckBox的属性即可:
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyCheckBox"/>