为什么切换textOn和textOff没有显示?

时间:2015-06-09 07:49:04

标签: android

我正在尝试使用开/关开关制作列表视图。我发现这个简单的代码,但问题是它不适合我。

<Switch
    android:id="@+id/itemListSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"/>

是的,它显示一个开关,但文本(振动打开/关闭)不是。仅显示可以切换的圆圈。我想发布截图但我不被允许,因为我缺乏声誉。任何人都可以告诉我为什么,因为我试图找到答案,但像上面的一个简单的代码正在为他们工作。如果有帮助,我的Android手机版本是5.0.2。提前谢谢!

2 个答案:

答案 0 :(得分:8)

首先,您应该使用SwitchCompat,以使其与所有Android版本兼容,并具有开关材料设计的漂亮外观。

回到你的问题,你缺少一个显示文字的属性 - app:showText - ,这是一个例子:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  ...>
    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_compat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="50dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="25dp"
        android:checked="false"
        android:textOff="OFF"
        android:textOn="ON"
        app:showText="true"/>
</RelativeLayout>

答案 1 :(得分:1)

默认情况下,“材质主题”下不显示文字。

您可以使用android:showText属性

进行更改
<Switch
    android:id="@+id/itemListSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"
    android:showText="true" />