改造Switch为IOS风格

改造Switch的样式为IOS效果

布局

<androidx.appcompat.widget.SwitchCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:checked="false"
        android:thumb="@drawable/switch_button_thumb"
        app:track="@drawable/switch_button_track" />

thumb 和 track 需要自定义。

thumb

switch_button_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">

        <shape android:shape="oval">
            <solid android:color="#ffffff" />
            <size
                android:width="18dp"
                android:height="18dp" />
        </shape>
    </item>
</layer-list>

使用layer-list,这样在item里面可以设置上下左右的间隙,如果不设置这个间距,就达不到效果。

track

switch_button_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <corners android:radius="12dp" />
            <solid android:color="#E61045" />
            <size android:width="36dp" android:height="20dp" />
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <corners android:radius="12dp" />
            <solid android:color="#eeeeee" />
            <size android:width="36dp" android:height="20dp" />
        </shape>
    </item>
</selector>

设置选中和非选中两个状态的样式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,可以使用Switch控件来实现类似iOS的美化效果。下面是一种实现方式: 1. 首先,在XML布局文件中添加Switch控件: ```xml <Switch android:id="@+id/switch_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/switch_thumb" android:track="@drawable/switch_track" /> ``` 2. 创建两个drawable资源文件,用于设置Switch的样式。例如,switch_thumb.xml和switch_track.xml。 - switch_thumb.xml:用于设置Switch的滑块样式。 ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/switch_thumb_on" android:state_checked="true" /> <item android:drawable="@drawable/switch_thumb_off" /> </selector> ``` - switch_track.xml:用于设置Switch的背景样式。 ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/switch_track_on" android:state_checked="true" /> <item android:drawable="@drawable/switch_track_off" /> </selector> ``` 3. 创建四个drawable资源文件,用于设置Switch的不同状态下的样式。例如,switch_thumb_on.xml、switch_thumb_off.xml、switch_track_on.xml和switch_track_off.xml。 4. 在Java代码中,可以通过findViewById方法获取Switch控件的实例,并对其进行操作。例如,监听Switch的状态变化: ```java Switch switchButton = findViewById(R.id.switch_button); switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 处理Switch状态变化的逻辑 } }); ``` 以上是一种实现Android Switch仿iOS美化的方式。你还有其他相关问题吗?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值