【发布时间】:2017-03-01 11:46:39
【问题描述】:
【问题讨论】:
-
您是如何做到这一点的?你能分享你的答案吗
标签: android custom-controls switchcompat
【问题讨论】:
标签: android custom-controls switchcompat
这是一个很好的例子:
您可以定义用于背景的drawable,以及像这样的切换器部分:
<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>
这定义了拇指可绘制对象,即在背景上移动的图像。滑块使用了四个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>
停用的版本:
重点版本:
和默认版本:
要拥有一个样式开关,只需创建这两个选择器,将它们设置为您的开关视图,然后将七个图像更改为您想要的样式。
【讨论】: