AndroidUI控件switch使用方法

首先添加控件:

<Switch

    android:id="@+id/sw_sfktmmzf"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginRight="15dp"

    android:showText="false"

    android:switchMinWidth="50dp"

    android:thumb="@drawable/thumb"

    android:track="@drawable/track" />

以下是该控件的常用属性:

textOn:控件打开时显示的文字

textOff:控件关闭时显示的文字

thumb:控件开关的图片(设置小圆圈颜色)

track:控件开关的轨迹图片(设置小圆圈背景颜色)

typeface:设置字体类型

switchMinWidth:开关最小宽度

switchPadding:设置开关 与文字的空白距离

switchTextAppearance:设置文本的风格

checked:设置初始选中状态

splitTrack:是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)

showText:设置是否显示开关上的文字(API 21及以上)

创建北京控制文件在drawable文件下

1、thumb.xml

<?xml version="1.0" encoding="utf-8"?><!-- 按钮的选择器,可以设置按钮在不同状态下的时候,按钮不同的颜色 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/green_thumb" android:state_checked="true" />

    <item android:drawable="@drawable/gray_thumb" />

颜色文件:

green_thumb.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >

    <!-- 高度40 -->

    <size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>

    <!-- 圆角弧度 20 -->

<corners android:radius="20dp"/>

<!-- 变化率 -->

    <gradient

        android:endColor="#eeeeee"

        android:startColor="#eeeeee" />

<stroke android:width="1dp"

        android:color="@color/home_text1"/>

</shape>

gray_thumb.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle" >

    <!-- 高度40 -->

    <size android:height="@dimen/switch_height" android:width="@dimen/switch_height"/>

    <!-- 圆角弧度 20 -->

<corners android:radius="20dp"/>


<!-- 变化率 -->

    <gradient

        android:endColor="#eeeeee"

        android:startColor="#eeeeee" />


    <stroke android:width="1dp"

        android:color="@color/text_color03"/>

</shape>

2、track.xml

<?xml version="1.0" encoding="utf-8"?><!-- 底层下滑条的样式选择器,可控制Switch在不同状态下,底下下滑条的颜色 -->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/green_track" android:state_checked="true" />

    <item android:drawable="@drawable/gray_track" />

</selector>

颜色文件:

green_track.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!-- 高度40 -->

    <size android:height="@dimen/switch_height"/>

    <!-- 圆角弧度 20 -->

    <corners android:radius="15dp"/>

    <!-- 变化率 -->

    <gradient

        android:endColor="@color/home_text1"

        android:startColor="@color/home_text1" />

</shape>

gray_track.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="rectangle">

    <!-- 高度30  此处设置宽度无效-->

    <size android:height="@dimen/switch_height" />

    <!-- 圆角弧度 15 -->

    <corners android:radius="15dp" />

    <!-- 变化率  定义从左到右的颜色不变 -->

    <gradient

        android:endColor="@color/text_color03"

        android:startColor="@color/text_color03" />

</shape>


switch 控件监听事件:

 aSwitch.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

      @Override

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        //控制开关字体颜色

        if(isChecked) {

         //打开

        }else{

         //关闭

        }

      }

    });

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容