gpt4 book ai didi

android - 具有不同状态图像的自定义 switchcompat

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:13 25 4
gpt4 key购买 nike

使用列表和 map 数据演示开发应用程序。要在演示文稿之间切换,应使用带有图像的自定义切换,如下所示:

enter image description here

如何创建这样的自定义switchcompat?

最佳答案

这是一个很好的例子:

ORIGINAL ANSWER

您可以像这样定义用于背景的可绘制对象和切换器部分:

<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用于 slider 的图像:

停用版本(Android 使用的 xhdpi 版本) The deactivated version
按下的 slider : The pressed slider
激活的 slider (开启状态): The activated slider
默认版本(关闭状态): enter image description here

在下面的选择器中定义了三种不同的背景状态:

<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>

停用版本: The deactivated version
重点版本: The focused version
和默认版本: the default version

要设置样式开关,只需创建这两个选择器,将它们设置为您的开关 View ,然后将七个图像更改为您想要的样式。

关于android - 具有不同状态图像的自定义 switchcompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42530894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com