【问题标题】:Custom switch in Android with text in trackAndroid中带有文本的自定义开关
【发布时间】:2017-10-10 09:24:30
【问题描述】:

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

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

我还想为轨道添加一些填充,我不希望拇指触摸轨道。 这是我想要的样子:

这是我的轨迹的形状:

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

【问题讨论】:

    标签: java android xml uiswitch


    【解决方案1】:

    关闭屏幕截图

    开启截图

    打开图片

    关闭图片

    缩略图

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

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

    【讨论】:

    • 这是可能的,但我没有我设计的特定图像。我使设计可以在没有文字的情况下绘制。我只需要添加文本。
    • 嗯,AFAIK,在轨道中设置文本是不可能的,参考这个链接,也许它可以解决你的查询:-stackoverflow.com/questions/34765660/…
    • 我接受这个答案,因为它是迄今为止最好的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多