android switch控件_安卓开发入门教程UI控件_Switch

什么是Switch

Switch是一种用于显示开关状态的UI控件.

基础样例

1.普通开关

效果图

640?wx_fmt=png

代码

<Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是Switch" />

2.设置字号

效果图

640?wx_fmt=png

代码

<Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是20号字体的Switch"android:textSize="20sp" />

3.设置颜色

效果图

640?wx_fmt=png

代码

<Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是红色的Switch"android:textColor="#FF0000" />

4.设置加粗

效果图

640?wx_fmt=png

代码

<Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="我是加粗的Switch"android:textStyle="bold" />

5.设置选中

效果图

640?wx_fmt=png

代码

<Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="我是被选中的Switch" />

5.设置开关按钮文字

效果图

640?wx_fmt=png

640?wx_fmt=png

代码

<Switchandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:showText="true"android:text="开关上显示文字"android:textOff="已关闭"android:textOn="已打开" />

基础样例完整源代码

https://gitee.com/cxyzy1/SwitchDemo

常用属性说明

属性名用途
android:layout_width设置控件宽度,可设置为:match_parent(和父控件一样),wrap_content(按照内容自动伸缩),设置固定值(如200dp)
android:layout_height设置控件高度,可设置为:match_parent(和父控件一样),wrap_content(按照内容自动伸缩),设置固定值(如200dp)
android:gravity控件内对齐方式
android:background设置背景,可以是色值(如#FF0000)或图片等
android:visibility可选值: visible(显示), invisible(隐藏,但是仍占据UI空间),gone(隐藏,且不占UI空间)
android:text设置文本内容
android:textSize设置字号
android:textColor设置颜色
android:textStyle设置字体样式,可选值:normal(正常),bold(加粗),italic(斜体)
android:checked是否选中,可选值:true(选中),false(未选中)
android:textOff设置开关未选中时显示的文字
android:textOn设置开关选中时显示的文字
android:showText设置是否显示开关上文字
android:switchMinWidth设置开关的最小宽度
android:track设置底部图片
android:thumb设置滑块图片

更多属性及实际效果,可以在开发工具里自行体验.

关注头条号,第一时间获取最新文章:

640?wx_fmt=png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值