扣扣技术交流群:460189483

目录:
    1.应用场景与概述
    2.常用属性
    3.简单使用
    4.更改默认Switch的样式
    5.自定义Switch
    
1.应用场景与概述
    Switch是在4.0以后推出的,所以要注意开发时的minsdk设置,google在API 21后也推出support v7 包下的SwitchCompa的Material Design
开关控件,对低版本的有了更好的的支持。其实switch的应用场景和ToggleButton类似,多应用于两种状态的切换。
    
2.常用属性

    android:typeface="normal":设置字体类型android:track="":设置开关的轨迹图片android:textOff="开":设置开关checked的文字android:textOn="关":设置开关关闭时的文字android:thumb="":设置开关的图片android:switchMinWidth="":开关最小宽度android:switchPadding="":设置开关 与文字的空白距离android:switchTextAppearance="":设置文本的风格android:checked="":设置初始选中状态android:splitTrack="true":是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)android:showText="true":设置是否显示开关上的文字(API 21及以上)

简单设置:

<pre name="code" class="html">   <Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textOff=""android:textOn=""android:switchMinWidth="120dp"android:thumb="@android:color/transparent"android:track="@drawable/switch_track"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/switch_close" android:state_checked="false" /><item android:drawable="@drawable/switch_open" android:state_checked="true" />
</selector>

效果展示:

这里layout_width:这能设置整个布局的宽度,不能设置具体的Switch的大小,需要使用switchMinWidth属性来设置。

thumb:文字所携带的背景,设置为背景色进行隐藏。不设置会出现一个背景框。

track:设置开关的背景图片,类似于button的background。

textoff、texton:设置开关时的文字显示。
   Switch的点击事件:

    private Switch mSwitch;private TextView mText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSwitch = (Switch) findViewById(R.id.switch_);mText = (TextView) findViewById(R.id.text_);// 添加监听mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mText.setText("开启");}else {mText.setText("关闭");}}});}

3.简单使用
    3.1)主布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.aswitch.MainActivity"><!--android:typeface="normal":设置字体类型android:track="":设置开关的轨迹android:textOff="开":设置开关checked的文字android:textOn="关":设置开关关闭时的文字android:thumb="":设置开关的图片android:switchMinWidth="":开关最小宽度android:switchPadding="":设置开关 与文字的空白距离android:switchTextAppearance="":设置文本的风格android:checked="":设置初始选中状态android:splitTrack="true":是否设置一个间隙,让滑块与底部图片分隔--><TextViewandroid:id="@+id/switch_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="switch:" /><Switchandroid:layout_marginTop="10dp"android:layout_below="@+id/switch_tv"android:id="@+id/switch1"android:typeface="normal"android:textOff="开"android:textOn="关"android:switchMinWidth="40dp"android:switchPadding="10dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text"android:layout_marginTop="10dp"android:layout_below="@+id/switch1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" /><TextViewandroid:layout_below="@+id/text"android:id="@+id/switch_compat_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="switchCompat:" /><android.support.v7.widget.SwitchCompatandroid:layout_marginTop="10dp"android:layout_below="@+id/switch_compat_tv"android:id="@+id/switch_compat"android:typeface="normal"android:switchMinWidth="40dp"android:switchPadding="10dp"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text1"android:layout_marginTop="10dp"android:layout_below="@+id/switch_compat"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" />
</RelativeLayout>

3.2)主布局java类

package com.example.aswitch;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{private Switch aSwitch;private SwitchCompat aSwitchCompat;private TextView text1,text2,switchText,switchCompatText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//实例化aSwitch = (Switch) findViewById(R.id.switch1);aSwitchCompat = (SwitchCompat) findViewById(R.id.switch_compat);text1 = (TextView) findViewById(R.id.text);text2 = (TextView) findViewById(R.id.text1);//设置Switch事件监听aSwitch.setOnCheckedChangeListener(this);aSwitchCompat.setOnCheckedChangeListener(this);}/*继承监听器的接口并实现onCheckedChanged方法* */@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {switch (buttonView.getId()){case R.id.switch1:if(isChecked){text1.setText("开");}else {text1.setText("关");}break;case R.id.switch_compat:if(isChecked){text2.setText("开");}else {text2.setText("关");}break;default:break;}}
}

3.3)截图效果

4.更改默认Switch的样式
    4.1)在styles.xml中自定义style

    <!--自定义switch的按钮和轨迹颜色theme-->  <style name="mySwitch" parent="Theme.AppCompat.Light"><!-- switch 打开时的按钮的颜色 轨迹颜色默认为30%(看效果就明白30%是怎么回事了)这个颜色 --><item name="colorControlActivated">@android:color/holo_green_dark</item><!--  switch关闭时的按钮的颜色 --><item name="colorSwitchThumbNormal">@color/colorAccent</item><!-- switch关闭时的轨迹的颜色 30%这个颜色 --><item name="android:colorForeground">@color/colorPrimaryDark</item></style>    

4.1)在布局文件中通过android:theme="@style/mySwitch"设置

   <android.support.v7.widget.SwitchCompatandroid:layout_marginTop="10dp"android:layout_below="@+id/switch_compat_tv"android:id="@+id/switch_compat"android:typeface="normal"android:theme="@style/mySwitch"android:switchMinWidth="40dp"android:switchPadding="10dp"android:layout_width="wrap_content"android:layout_height="wrap_content" />    

5.自定义Switch

5.1)导入资源图片thumb.png ,thumb_on.png ,track_nomal.png ,track_on.png ,track_press.png
    
    5.2)实现thumb_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!--选中时的滑块图片-->
<item android:drawable="@drawable/thumb_on" android:state_checked="true"/><!--正常情况滑块图片-->
<item android:drawable="@drawable/thumb"/>
</selector>

5.3)实现track_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><!--打开时switch轨迹图片--><item android:state_pressed="true"  android:drawable="@drawable/track_on" /><!--按压时switch轨迹图片--><item android:state_checked="true"  android:drawable="@drawable/track_press" /><!--正常状态switch轨迹图片--><item                               android:drawable="@drawable/track_nomal" /></selector>

5.4)主布局actiity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.aswitch.SecondActivity"><TextViewandroid:id="@+id/CustomSwitchCompat_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="CustomSwitchCompat:" /><android.support.v7.widget.SwitchCompatandroid:layout_marginTop="10dp"android:layout_below="@+id/CustomSwitchCompat_tv"android:id="@+id/CustomSwitchCompat"android:layout_width="wrap_content"android:minWidth="40dp"android:minHeight="20dp"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/custom_result"android:layout_marginTop="10dp"android:layout_below="@+id/CustomSwitchCompat"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" />
</RelativeLayout>

5.5)主布局java类SecondActivity.java

package com.example.aswitch;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;public class SecondActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{private SwitchCompat customSwitchCompat;private TextView custom_result,CustomSwitchCompat_tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//实例化customSwitchCompat = (SwitchCompat) findViewById(R.id.CustomSwitchCompat);custom_result = (TextView) findViewById(R.id.custom_result);//设置自定义的thumb和trackcustomSwitchCompat.setThumbResource(R.drawable.thumb_selector);customSwitchCompat.setTrackResource(R.drawable.track_selector);//设置Switch事件监听customSwitchCompat.setOnCheckedChangeListener(this);}/*继承监听器的接口并实现onCheckedChanged方法* */@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked){custom_result.setText("开");}else {custom_result.setText("关");}}
}

ps:其实自定义的途径还可以通过shape的绘制和java代码绘制,在这里就不详细说了
    
    参考:http://blog.csdn.net/zhyh1986/article/details/45406391

Android 开关控件Switch相关推荐

  1. android开关控件Switch和ToggleButton

    序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下. 1.Switch <Switchandroid:id="@+id/bt"android:layout ...

  2. Bootstrap Switch 开关控件

    Bootstrap Switch开关控件网址:http://www.bootcss.com/p/bootstrap-switch/ 引入. Bootstrap Switch插件和依赖插件,并初始化开关 ...

  3. Android 基础 View 系列之 仿IPhone 开关控件

    极力推荐Android 开发大总结文章:欢迎收藏程序员Android 力荐 ,Android 开发者需要的必备技能 自定义View 是Android中常用的方法之一,本章实现类似于IPhone 开关控 ...

  4. Android自己写的三款实用开关控件

    2019独角兽企业重金招聘Python工程师标准>>> 自定义开关控件,代码简单,比较实用. http://www.see-source.com/androidwidget/list ...

  5. android 表格控件点击事件,Android零基础入门|RecyclerView点击事件处理

    原标题:Android零基础入门|RecyclerView点击事件处理 前面两期学习了RecyclerView的简单使用,并为其item添加了分割线.在实际运用中,无论是List还是Grid效果,基本 ...

  6. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  7. Android之控件使用

    Android系统为我们提供了大量的控件,例如:开关控件.单选按钮.多选按钮.单选菜单等等,那么这些控件如何使用呢?本篇我将带领大家一道学习一下如何使用这些控件.所谓无图无真相,先让大家看一下效果图: ...

  8. 日历控件的android代码,Android日历控件PickTime代码实例

    Android日历控件PickTime代码实例 发布时间:2020-10-03 16:05:51 来源:脚本之家 阅读:86 作者:手撕高达的村长 最近做项目,需要设置用户的生日,所以做这样一个功能. ...

  9. weui-switch开关控件,表单提交后如何取值

    最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...

最新文章

  1. yourtour的几种链接
  2. 双稳态电路的两个稳定状态是什么_干货|常见的脉冲电路到底有何用途和特点?终于了解了!|脉冲|晶体管|双稳|单稳|振荡器...
  3. C++基于多态实现依赖颠倒原则附源码
  4. (转)RemoteView 设置控件属性
  5. qt ui界面加入qsplitter_UI 文件设计与运行机制
  6. mysql在linux下显示花_在Linux 中搭建 Mysql
  7. rtmp流\http流测试地址
  8. 阿里云混合云Apsara Stack 2.0发布 加速政企数智创新
  9. matlab取矩阵实部和虚部,MATLAB中容易忽略却经常遇到的小技巧总结
  10. Angular4.x+Ionic3 踩坑之路之打包时出现JAVASCRIPT HEAP OUT OF MEMORY的几种解决办法
  11. 查看nginx进程_nginx的进程模型与配置
  12. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月5日-8月12日)
  13. 【Java必备技能二】防止表单重复提交方法
  14. 【UVM源码】uvm_event
  15. 今天使用overleaf生成个人简历
  16. QPainter详解
  17. SQL注入的防范措施
  18. python基础:面向对象的应用--搬家具。
  19. 上升了百分之几怎么算_如何简单计算同期上升下降的百分比?
  20. 基于uniapp的校园社区小程序

热门文章

  1. hangfire的使用
  2. DDL(数据定义语言)讲解
  3. python宇晨_第三十届全国青少年科技创新大赛青少年科技创意作品中学组.PDF
  4. IBM X3400 m3 EFI platform initialization
  5. 【LINUX-python】PATH、sys.path、PYTHONPATH
  6. c语言打铃器单片机程序,基于单片机的自动打铃器的设计
  7. Docker11_1:Docker阿里云仓库
  8. java 为什么要get,set方法
  9. 一文详解基因组denovo组装原理和实战
  10. signature=d363d26bda212f777fef81d270ecd42b,基于DNA-pooling全基因组重测序初步筛查CAD易感基因变异位点...