Android中自定义Checkbox

  • Post author:
  • Post category:IT
  • Post comments:0评论

在Android中,Checkbox是一个很重要的UI组件,而且在Android中,它展现的形式越来越好看,这就说明有些系统,比如4.0以下,checkbox还是比较不好看,或者跟软件的风格不协调,就需要我们自定义这个组件。

自定义这个组件很简单,简单的增加修改xml文件即可。

准备工作

准备好两张图片,一个是选中的图片,另一个是未选中的图片。本文以checked.png和unchecked.png为例。

设置选择框

在drawable下新建文件custom_checkbox.xml

1
2
3
4
5
6
<?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/checked"></item>
  <item android:state_checked="false" android:drawable="@drawable/unchecked"></item>
  <item android:drawable="@drawable/unchecked"></item><!-- The default one -->
</selector>

应用自定义

设置button属性值为上面定义的custom_checkbox。

1
2
3
4
5
<CheckBox
  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_checkbox"
/>

自定义完毕,跑起来你的程序吧。

其他

发表回复