android 控件之checkbox自定义样式解析

虾米哥 阅读:243 2021-03-31 22:49:43 评论:0

1.首先在drawable文件夹中添加drawable文件checkbox_style.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文件中添加MyCheckBox样式。

<resources> 
 
    <!-- 
        Base application theme, dependent on API level. This theme is replaced 
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
 
    --> 
    <style name="AppBaseTheme" parent="android:Theme.Light"> 
        <!-- 
            Theme customizations available in newer API levels can go in 
            res/values-vXX/styles.xml, while customizations related to 
            backward-compatibility can go here. 
 
        --> 
    </style> 
 
    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
        <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    </style> 
 
    <span style="color:#ff0000;"><style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> 
        <item name="android:button">@drawable/checkbox_selector</item> 
        <item name="android:paddingLeft">25.0dip</item> 
        <item name="android:maxHeight">10.0dip</item> 
    </style></span> 
 
</resources>


3.在布局文件使用MyCheckBox样式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20sp" 
        android:layout_marginRight="10sp" 
        android:orientation="horizontal" > 
 
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="@string/sports" 
            android:textSize="18sp" > 
        </TextView> 
 
       <span style="color:#ff0000;"> <CheckBox 
            style="@style/MyCheckBox" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" /></span> 
         
         <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="@string/distance" 
            android:textSize="18sp" > 
        </TextView> 
    </LinearLayout> 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10sp" 
        android:layout_marginRight="10sp" 
        android:orientation="horizontal" > 
 
        <Button 
            android:layout_width="24sp" 
            android:layout_height="24sp" 
            android:background="@drawable/reduce" > 
        </Button> 
 
        <SeekBar 
            android:id="@+id/seekbar" 
            android:layout_width="250sp" 
            android:layout_height="24sp" 
            android:max="30" 
            android:progress="10" 
            android:progressDrawable="@drawable/bg_bar" 
            android:thumb="@drawable/thumb_bar" /> 
 
        <Button 
            android:layout_width="24sp" 
            android:layout_height="24sp" 
            android:background="@drawable/add" > 
        </Button> 
    </LinearLayout> 
 
</LinearLayout>

相关图片资源:

check_pressed:

check_normal:


界面截图:


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们