我需要在安卓上制作一个类似下面给出的圆角矩形切换开关:
有人能指导我完成这个开关的完整步骤吗?
我按照以下方式解决了我的问题:
在我的xml布局文件中添加了一个切换按钮:
<ToggleButton
android:id="@+id/ToggleButton1"
android:layout_width="120dp"
android:layout_height="25dp"
android:layout_marginRight="30dp"
android:layout_weight="2"
android:background="@drawable/toogle_switch"
android:text="ToggleButton"
android:textOff=""
android:textOn="" />
然后在“drawable”文件夹中定义了一个名为“toogle_switch”的自定义切换按钮背景,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switchon" android:state_checked="true"></item>
<item android:drawable="@drawable/switchoff" android:state_checked="false"></item>
</selector>
switchon和switchoff是我在问题中展示的两个图像。
希望这能帮助大家!:)
给你:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
显示的确切图像由所谓的“选择器”或“状态列表”确定,它是将按钮状态映射到图像的XML片段。
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
你需要:android:background
属性