ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android 的Switch控件的android:thumb 和android:track属性的理解

2022-03-30 16:04:38  阅读:404  来源: 互联网

标签:xml 控件 滑道 thumb 滑块 track android


有些人对Switch控件的 android:thumb和 android:track两个属性理解不清楚和明白,现在跟大家讲清楚这两个属性的作用。

Switch主要有两个属性构成:上面圆形的滑块,下面的滑道

android:thumb对应的滑块

android:track 对应的滑道。

1、先来看Switch默认样式,如下:

    checked=false,滑块和滑道都是灰色。

 

      checked=true,滑块是深粉红色,滑道是浅粉红色

对应的xml:

<Switch
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Switch>

2、设置滑道样式,如图:

  checked=false,滑块还是系统默认颜色白色,滑道变成深灰色。

  checked=true,滑块还是系统默认颜色深粉红色,滑道变成浅绿色。

 

样式和xml代码:

在资源的drawable目录下创建switch_track_off.xml 文件,如下:

 

switch_track_off.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="@android:color/darker_gray">
    </solid>
    <corners
        android:radius="30dp">
    </corners>
</shape>

switch_track_on.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="@android:color/holo_green_light">
    </solid>

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

</shape>

switch_track.xml 文件

<?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/switch_track_on"></item>
    <item
        android:state_checked="false"
        android:drawable="@drawable/switch_track_off"></item>

</selector>

布局文件:

    <Switch
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:track="@drawable/switch_track"></Switch>

3、设置滑块样式:

    在第二步上,设置滑块样式,checked=false,滑块由系统默认的白色改为黑色,滑道还是第二步的灰色。

               checked=true,滑块由系统默认的深粉红色改为蓝色,滑道还是第二步的绿色。

 

 

在资源的drawable目录下创建switch_thumb_off.xml 文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="30dp"
        android:height="30dp">
    </size>
    <solid
        android:color="#000000">
    </solid>

</shape>

switch_thumb_on.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="30dp"
        android:height="30dp">
    </size>
    <solid
        android:color="#0000ff">
    </solid>

</shape>

switch_thumb.xml :

<?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/switch_thumb_on"></item>
    <item
        android:state_checked="false"
        android:drawable="@drawable/switch_thumb_off"></item>
</selector>

布局文件:

    <Switch
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:track="@drawable/switch_track"
        android:thumb="@drawable/switch_thumb"></Switch>

 

上面已经把Switch 的android:thumb和 android:track属性讲清楚了。

 

标签:xml,控件,滑道,thumb,滑块,track,android
来源: https://www.cnblogs.com/zoro-zero/p/16077460.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有