2
A
回答
0
这里是很好的例子:
您可以定义用作背景的图形内容和这样的切换器部分:
<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>
停用的版本:
聚焦的版本:
和默认版本:
为了有一个风格的切换只需创建此两个选择,它们设置为你的交换机查看,然后将这七个图像更改为所需的样式。
谢谢你的回答!但这个控件应该像SwitchCompat(不像单选按钮) - 当用户从左向右拉时,应该改变状态,反之亦然 –
@ElviraIatsko我编辑了我的answear –