gpt4 book ai didi

java - Android 中带有文本的自定义开关

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:21 27 4
gpt4 key购买 nike

我有一个自定义开关。我希望文本(开/关)位于轨道中而不是拇指中。

我正在考虑在可绘制的文本中设置一个选择器并将其设置为开关的轨道。但是我不能在形状中设置文本。我有什么想法可以做到这一点吗?

我还想给轨道添加一些填充,我不想让拇指碰到轨道。这就是我想要的样子:

I want the switch to be kind of like this

这是我的轨道形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true"
android:useLevel="false">
<corners
android:radius="20dp" />
<size
android:width="48dp"
android:height="21dp" />
<stroke
android:color="#bfbfbf"
android:width="6dp"/>

最佳答案

关闭截图

enter image description here

开启截图

enter image description here

打开图像

enter image description here

关闭图像

enter image description here

缩略图

enter image description here

switch_on_off.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/on_switch"/>
<item android:state_checked="false" android:drawable="@drawable/off_switch"/>
<item android:drawable="@drawable/off_switch"/>
</selector>

xml代码

<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:track="@drawable/switch_on_off"
android:thumb="@drawable/on_switch_active"/>

尝试上面的代码,如果发现任何错误请告诉我......

关于java - Android 中带有文本的自定义开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43914326/

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