一.所花时间

0.5h

二.代码量

20行

三.博客量

1篇

四.了解到的知识点

 开关按钮Switch 
Switch是开关按钮,它像一个高级版本的CheckBox,在选中与取消选中时可展现的界面元素比复选框丰
富。Switch控件新添加的XML属性说明如下:
textOn:设置右侧开启时的文本。
textOff:设置左侧关闭时的文本。
track:设置开关轨道的背景。
thumb:设置开关标识的图标。
虽然开关按钮是升级版的复选框,但它在实际开发中用得不多。

现在要让Android实现类似iOS的开关按钮,主要思路是借助状态列表图形,首先创建一个图形专用的
XML文件,给状态列表指定选中与未选中时候的开关图标,如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:drawable="@drawable/switch_on"/>
 <item android:drawable="@drawable/switch_off"/>
 </selector>

然后把CheckBox标签的background属性设置为@drawable/switch_selector,同时将button属性设置
为@null。

<CheckBox
 android:id="@+id/ck_status"
 android:layout_width="60dp"
 android:layout_height="30dp"
 android:background="@drawable/switch_selector"
 android:button="@null" />

 

posted on 2024-05-10 15:54  leapss  阅读(21)  评论(0)    收藏  举报