自定义checkbox 样式



有些时候需要根据项目需求自定义CheckBox 的样式,步骤如下:


1.首先在res / drawable文件夹中添加drawable文件my_checkbox.xml

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

    <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/><!-- 选中状态 -->
    <item android:drawable="@drawable/checkbox_default" android:state_checked="false"/><!-- 未选中 -->
    <item android:drawable="@drawable/checkbox_default"/><!-- 默认正常状态 -->

</selector>

2.在CheckBox中使用my_checkbox

                <CheckBox
                    android:id="@+id/checkBox"
                    style="@android:style/Widget.CompoundButton.CheckBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:button="@drawable/my_checkbox"
                    android:text="平安出行保险 普通型" />



使用的图片资源
未选中状态
的图片
选中状态
的图片
   
   
   
   
   
   
  




发布了47 篇原创文章 · 获赞 60 · 访问量 16万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章