您的位置:首页 > 移动开发 > Android开发

android 控件之checkbox自定义样式

2014-12-03 19:39 681 查看
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:


界面截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: