android 自定義checkbox 樣式

jopen 10年前發布 | 108K 次閱讀 Android Android開發 移動開發

有些時候需要根據項目需求自定義CheckBox 的樣式,步驟如下:

1.首先在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_pressed" android:state_checked="true"/>  
        <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>  
        <item android:drawable="@drawable/checkbox_normal"/>  

    </selector>  

 

2.在values文件夾下的styles.xml文件中添加CustomCheckboxTheme樣式

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


3.在CheckBox中使用CustomCheckboxTheme樣式

    <CheckBox  
            android:id="@+id/checked"  
            style="@style/custom_checkbox"  
            android:layout_width="40dip"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="10dip"  
            android:checked="false" />  


使用的圖片資源

checkbox_normal.png 未選中狀態圖片

g1.png

checkbox_pressed.png 選中狀態圖片

g2.png

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!