隐藏android开关按钮的默认文本

时间:2015-02-18 14:55:38

标签: android button switch-statement

在我的Android应用程序中,我使用了切换按钮,无论如何都要隐藏切换按钮的文本,对于API级别21,有一个隐藏选项(android:showText)但我需要在较低级别的设备上运行它 下面是我的xml:

<Switch
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:background="@drawable/offbuttonbg"
        android:textAppearance="@style/SwitchTextAppearance"
        android:thumb="@drawable/switchselector" />

2 个答案:

答案 0 :(得分:13)

对于较低版本,要隐藏文字使用

 <Switch
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"
            android:textOff=""
            android:textOn=""
            android:background="@drawable/offbuttonbg"
            android:textAppearance="@style/SwitchTextAppearance"
            android:thumb="@drawable/switchselector" />

答案 1 :(得分:0)

您有两种选择,一种是通过代码,另一种是通过XML。

通过代码:

yourSwitch.setTextOff("textWhenOff") //Sets the text when the component is not in checked state.

yourSwitch.setTextOn("textWhenOn") //Sets the text when the component is not in checked state.

<强> XML

<Switch
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:textOff="The text for the component when it's not checked."
        android:textOn="The text for the component when it's checked." 
        android:background="@drawable/offbuttonbg"
        android:textAppearance="@style/SwitchTextAppearance"
        android:thumb="@drawable/switchselector" />

如果您需要更多信息,请务必参阅Switch Documentation