您可以定义用作背景的图形内容和切换器部分是这样的:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/switch_thumb"
android:track="@drawable/switch_bg" />
现在,你需要创建一个选择用于定义切换绘制的不同状态。 这里从Android源的副本:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_thumb_disabled_holo_light" />
<item android:state_pressed="true" android:drawable="@drawable/switch_thumb_pressed_holo_light" />
<item android:state_checked="true" android:drawable="@drawable/switch_thumb_activated_holo_light" />
<item android:drawable="@drawable/switch_thumb_holo_light" />
</selector>
这定义了拇指可绘制,即上方移动背景的图像。有用于滑块4个ninepatch图像:
失活的版本(即Android是使用xhdpi版本)
压制滑块:
活化的滑块(接通状态):
缺省版本(关闭状态):
也有以下选择中定义为背景三种不同状态:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled_holo_dark" />
<item android:state_focused="true" android:drawable="@drawable/switch_bg_focused_holo_dark" />
<item android:drawable="@drawable/switch_bg_holo_dark" />
</selector>
停用的版本:
聚焦的版本:
和默认版本:
为了有一个风格的切换只需创建此两个选择,它们设置为你的交换机视图,然后更改7张图像到你想要的风格。
非常详细的感谢,可以让拇指搬出轨道的一部分9补丁滑块的图像是特别有帮助,因为我不能工作了他们是如何做。 (默认的滑块有尖的边缘位移过轨道末端以在一侧形成方形端) – 2012-04-15 23:54:26
很棒的工作 - 非常详细 – Ahmad 2012-04-30 16:39:07
还有一个状态是- 哪个当开关处于“开”状态时可以改变轨道图像。所以如果你想改变轨道上的“开”/“关”,使用这种状态。 –
narangrajeev81
2013-10-08 11:11:48