android之4.0控件switch自定义开关滑块、滑块轨道和宽度

来源:互联网 发布:八中数据谷实验中学 编辑:程序博客网 时间:2024/04/30 06:08
</pre><pre style="font-family:宋体; font-size:9.6pt; background-color:rgb(255,255,255)">区分一下概念:
1.android:background view背景
2.android:thumb 滑块 有选中状态 android:state_checked
3.android:track 滑块的轨道 有选中状态 android:state_checked
<Switch    android:id="@+id/customer_gender"    style="@style/widget_gender_switch" />


<style name="widget_gender_switch">

        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:textOff">""</item><!-- 此设置不使用文字,只用背景图的状态来表示开关 -->
        <item name="android:textOn">""</item>
        <item name="android:thumb">@drawable/widget_gender_thumb</item><!--滑块图,改变选中状态来表示开关-->
        <item name="android:thumbTextPadding">10dp</item><!-- 用来控制滑块和文字之间的间距来控制switch整体宽度 -->
        <item name="android:track">@drawable/widget_gender_track</item><!--滑块轨道,改变选中状态来表示开关-->

    </style>


widget_gender_track.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 开始按钮 -->    <item android:drawable="@drawable/track_checked_interview" android:state_checked="true" />    <item android:drawable="@drawable/track_nor_interview" /></selector>


widget_gender_thumb.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true" android:drawable="@drawable/abc_btn_switch_to_on_mtrl_00012" />    <item android:drawable="@drawable/abc_btn_switch_to_on_mtrl_00001" /></selector>


参考http://stackoverflow.com/questions/20762490/how-to-change-height-and-width-of-switch-in-android

http://stackoverflow.com/questions/10173590/how-to-change-the-size-of-a-switch-widget



1 2
原创粉丝点击