Android 中的自定义开关与轨道中的文本

Custom switch in Android with text in track

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

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

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

这是我的轨道形状:

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

关闭截图

开启截图

打开图像

关闭图像

缩略图

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

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