美文网首页
自定义开关Switch

自定义开关Switch

作者: 啤酒小龙虾 | 来源:发表于2019-02-27 17:23 被阅读0次
效果图

直接上代码

1. 定义按钮开的状态  switch_on

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

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

    android:shape="oval">

<solid android:color="#F64245" />

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

</shape>

2.定义按钮关的状态 switch_off

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

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

android:shape="oval">

<solid android:color="#A2A2A2" />

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

</shape>

3.创建按钮状态选择器

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

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

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

<item android:drawable="@drawable/switch_off" android:state_checked="false" />

</selector>

4.定义开关开启状态的背景色switch_bg_on

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

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

android:shape="rectangle">

<solid android:color="#ffffff" />

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

<stroke android:width="@dimen/dp_1" android:color="#F64245" />

<corners android:radius="@dimen/dp_13" />

</shape>

5.定义开关关闭状态的背景色switch_bg_off

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

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

android:shape="rectangle">

<solid android:color="#ffffff" />

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

<stroke android:width="@dimen/dp_1" android:color="#CFCFCF" />

<corners android:radius="@dimen/dp_13" />

</shape>

6.创建按钮背景状态选择器

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

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

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

<item android:drawable="@drawable/switch_bg_off" android:state_checked="false" />

</selector>

7.应用和其他设置

<Switch

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:switchMinWidth="@dimen/dp_46"

    android:showText="false"

    android:thumb="@drawable/switch_selector"

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

文字的显示与隐藏   android:showText="false"

调证控件大小 wigth 和 height 不起作用,所以用android:switchMinWidth 

相关文章

  • 自定义开关Switch

    直接上代码 1. 定义按钮开的状态switch_on

  • ToggleButton

    开关按钮ToggleButton和开关Switch

  • Android Switch开关自定义

    先不废话,上图 可以自定义滑块 和背景哦~ 2、开始进入代码环节 Java部分,监听和调用 3、switch控件属...

  • Android自定义开关Switch

    Java部分,监听和调用 3、switch控件属性介绍 4、xml部分代码 5、样式编写代码如下

  • switch开关

    1.UISwitch 创建 //创建UISwitch *switch1 = [[UISwitch alloc]in...

  • 笔记 || ele+vue3 表格1

    表格中嵌套switch开关: 根据字段:switch_status 的值,生成开关样式 以下办法只达到解决的效果,...

  • Android 自定义实现switch开关按钮

    在开发我们经常会使用到自定义实现控件, 今天呢就跟大家说一说switch开关按钮的自定义实现。 为了大家的方便,直...

  • 关于bootstrap中switch的那些坑

    1.今天做一个关于bootstrap中switch(开关)的功能,通过点击事件来改变switch(开关)disab...

  • 仿微信切换按钮

    因安卓自带的Switch不符合UI风格,所以应要求做一个类似微信的开关,颜色支持自定义。 自定义完后,不算复杂,主...

  • 仿微信切换按钮

    因安卓自带的Switch不符合UI风格,所以应要求做一个类似微信的开关,颜色支持自定义。 自定义完后,不算复杂,主...

网友评论

      本文标题:自定义开关Switch

      本文链接:https://www.haomeiwen.com/subject/joifuqtx.html