Android Switch控件修改样式

以上便是修改后效果 与 原生Switch的效果对比。代码在文章底部给出

实现方式:

1.底部滑动条,在开关打开状态为绿色,开关关闭状态为灰色

在 res/drawable 文件夹下面,写两个滑动条的底图 ,通过一个选择器selector进行控制。

gray_track.xml :非打开状态,灰色的底图

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

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

android:shape=“rectangle” >

<gradient

android:endColor=“#888888”

android:startColor=“#888888” />

green_track.xml:打开状态下,绿色的底图。

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

<gradient

android:endColor=“#33da33”

android:startColor=“#33da33” />

选择器 track.xml   用于控制Switch不同状态下,滑动条的底图

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

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

2. 滑动按钮:底色我用的接近白色的淡灰色,打开时,边上的一圈线条为灰色,关闭时,边上的一圈线条为绿色

实现方式和底部滑动一致

gray_thumb.xml  :关闭状态,按钮边上一圈颜色为深灰色

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

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

android:shape=“rectangle” >

<gradient

android:endColor=“#eeeeee”

android:startColor=“#eeeeee” />

<stroke android:width=“1dp”

android:color=“#666666”/>

green_thumb.xml : 打开状态,按钮边上一圈的颜色为绿色

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

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

android:shape=“rectangle” >

<gradient

android:endColor=“#eeeeee”

android:startColor=“#eeeeee” />

<stroke android:width=“1dp”

android:color=“#33da33”/>

选择器 thumb.xml   用于控制Switch不同状态下,按钮的显示状态

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

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

3. 将以上选择器设置给Switch,就好了

界面  activity_main.xml

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

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

<Switch

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:switchMinWidth=“20dp”

android:textOn="  "

android:textOff="  "

android:thumb=“@drawable/thumb”

android:track=“@drawable/track” />

<Switch

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值