自定義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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章