切换按钮在Android中不显示打开和关闭文本

切换按钮在Android中不显示打开和关闭文本,android,android-layout,android-switch,Android,Android Layout,Android Switch,我正在以编程方式创建一个开关按钮。我遇到的问题是,按钮没有显示开/关文本。这是创建代码: final RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); ll.addRule(Relativ

我正在以编程方式创建一个开关按钮。我遇到的问题是,按钮没有显示
开/关
文本。这是创建代码:

        final RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ll.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        sw.setLayoutParams(ll);
        sw.setTextOn(values[0].getName());
        sw.setTextOff(values[1].getName());
        values[0].getName() returns "OK", and values[1].getName() returns "NOK"
可能发生了什么

谢谢 詹姆

希望这会有所帮助

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:padding="5dp">

        <Switch
            android:id="@+id/mySwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp"
            android:text="Play with the Switch" />

        <TextView
            android:id="@+id/switchStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/mySwitch"
            android:layout_marginTop="22dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

您可以通过XML实现这一点

<Switch
...
android:showText="true" />

以下是安卓API的答案。你必须标记开关,它可以显示所有标签

API级别21中添加的公共void setShowText(布尔showText)

设置是否应显示开/关文本

相关的XML属性:

android:showText
参数 showText true可显示开/关文本


发布更多的代码,这样我们就可以知道什么是
sw
issw是切换对象。您可以通过RadioGroup和单选按钮来实现这一点。看看我对stackoverflow.com/questions/23358822/how to custom switch button/33231991#33231991问题的回答
mSwitch.setShowText(true);
android:showText