android - 创建自定义开关

标签 android android-layout android-switch

任何人都可以帮我创建一个如下所示的开关吗?

enter image description here

开关中的按钮也可以拖动到另一侧。当按钮从当前位置穿过一半时,它将在单击时改变位置。按钮中的文本也会更改。这是我指的:

enter image description here

我研究过 Android 上的开关,但没有一个给我这个结果。我还查看了切换按钮,但这也没有起到作用。

最佳答案

您在这个链接中得到了答案How to custom switch button? !

只需使用Selector来定义SwitchOn/Off状态,如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/switch_off"  />
    <item android:state_checked="true" android:drawable="@drawable/switch_on"  />
</selector>

关于android - 创建自定义开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315634/

相关文章:

android - 我应该为 Android 应用创建多大的尺寸?

android - 自定义安卓切换轨道 'animation'

java - 使用状态来管理 onTouchEvent

android - CollapsingToolbarLayout 中的多行 TextView 而不是标题

javascript - 在不同的设备上显示不同的图像

Android - layout_weight 使图像在两侧可点击

android - 为什么 Switch 在旧版 Android 上不显示?

android - 当用户未更新状态时如何跳过 switch.setOnCheckedChangeListener

android - RecyclerView 滚动不流畅

android - Android Kitkat (4.4) 的来电显示功能的新功能是否有 API?