android SwitchCompat开关控件使用

官网文档:https://developer.android.google.cn/reference/androidx/appcompat/widget/SwitchCompat

属性介绍:https://www.jianshu.com/p/796e2f7f0ed2

实现效果:

布局xml:

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switchLanguage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvTitle"
        android:layout_alignParentRight="true"
        android:textOff="繁"
        android:textOn="简"
        android:thumb="@drawable/switch_ios_thumb"
        app:showText="true"
        app:switchTextAppearance="@style/switchStyle"
        app:track="@drawable/switch_ios_track_selector" />
drawable新建文件:
switch_ios_thumb​​​​.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#FFF" />
    <stroke
        android:width="3dp"
        android:color="#00000000" />
    <size
        android:width="50dp"
        android:height="50dp" />
</shape>
switch_ios_track_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_ios_track_on" android:state_checked="true" />
    <item android:drawable="@drawable/switch_ios_track_off" android:state_checked="false" />
</selector>
switch_ios_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="#FD751F" />
    <corners android:radius="25dp"/>
</shape>
switch_ios_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="#E3E3E3" />
    <corners android:radius="25dp" />
</shape>

style.xml中添加switchStyle:

<style name="switchStyle" parent="Theme.AppCompat.Light">
        <item name="android:textSize">20sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#000000</item>
    </style>

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值