Android中的Switch控件

1、Switch中时Android中自带的开关控件:

效果如下:
在这里插入图片描述

2、完整代码如下所示:

 <Switch
            android:id="@+id/main_switch"
            android:layout_width="@dimen/x103"
            android:layout_height="@dimen/x3"
            android:track="@drawable/setting_button_track"
            android:textOn="@string/version_on"
            android:textOff="@string/version_off"
            android:thumb="@drawable/setting_button_thumb"
            android:layout_marginLeft="@dimen/x429"
            android:showText="true"
            android:switchTextAppearance="@style/SwitchTextAppearance"
            android:layout_gravity="center"
            tools:ignore="UseSwitchCompatOrMaterialXml" />

3、Switch的样式,文字编写都为自定义的,代码如下所示:

①setting_button_track

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/setting_on_track" />
    <item android:drawable="@drawable/setting_off_track" />
</selector>

(1)setting_on_track

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <solid android:color="@color/color_on"/>
    <corners android:radius="@dimen/x32"/>
    <size  android:width="@dimen/x48"
        android:height="@dimen/x24"/>

</shape>

(2)setting_off_track

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/color_off"/>
    <corners android:radius="@dimen/x32"/>
    <size  android:width="@dimen/x48"
        android:height="@dimen/x24"/>

</shape>

②setting_button_thumb

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/setting_off" />
    <item android:drawable="@drawable/setting_on" />
</selector>

(1)setting_off

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
<!--    <stroke android:width="1dp" android:color="@color/color_off"/>-->
    <solid android:color="@color/color_off"/>
    <size  android:width="@dimen/x32"
        android:height="@dimen/x32"/>

</shape>

(2)setting_on

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
    >
<!--<stroke android:width="1dp" android:color="@color/color_off"/>-->
<solid android:color="@color/color_on"/>
<size  android:width="@dimen/x32"
    android:height="@dimen/x32"/>
</shape>

4、switch中的属性

textOn:控件打开时显示的文字
textOff:控件关闭时显示的文字
thumb:控件开关的图片
track:控件开关的轨迹图片
typeface:设置字体类型
switchMinWidth:开关最小宽度
switchPadding:设置开关 与文字的空白距离
switchTextAppearance:设置文本的风格
checked:设置初始选中状态
splitTrack:是否设置一个间隙,让滑块与底部图片分隔
showText:设置是否显示开关上的文字(android系统中默认不显示)

5、文字样式

放置在value中

<style name="SwitchTextAppearance" parent="@android:style/TextAppearance.Holo.Small">
        <item name="android:textColor">#ffffff</item>
        <item name="android:textSize">@dimen/x12</item>
    </style>

对于出现switch的格式出现问题,开关状态出现遮盖等信息都是因为尺寸设置不正确。

6、注意事项

1、对于自定义的switch的样式来说,想要显示文字,必须加上showtext,否则无法显示

2、对于文字的样式,使用属性来加载switchTextAppearance=“@style/SwitchTextAppearance”

3、想要自定义switch的样式,要特别注意thumb和trick两个属性

4、在日常学习过程,保持良好代码命名规范

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>