您的位置:首页 > 移动开发 > Android开发

Android自定义CheckBox样式

2015-12-08 10:30 357 查看
Android提供了CheckBox控件,但是样式单一。例如,当背景为白色时,系统提供的Checkbox样式会让CheckBox隐藏在白色背景中。这时,我们需要用我们自己的资源图片自定义CheckBox样式。

1.在drawable文件夹中添加drawable文件hide_checkbox_style.xml

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

<item android:drawable="@drawable/hide_apps_checked" android:state_checked="true"/>
<item android:drawable="@drawable/hide_apps_unchecked" android:state_checked="false"/>
<item android:drawable="@drawable/hide_apps_unchecked"/>

</selector>


2.在values文件夹下的style.xml文件中添加HideCheckboxTheme样式

<style name="HideCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/hide_checkbox_style</item>
</style>


3.在CheckBox中使用HideCheckboxTheme样式

<CheckBox
android:id="@+id/app_hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
style="@style/HideCheckboxTheme"
android:layout
4000
_alignParentRight="true"
android:layout_centerVertical="true" />


效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: