Скрыть текст по умолчанию для переключателя android
В моем приложении для 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" />
Ответы
Ответ 1
Для более низкой версии, чтобы скрыть текст, используйте
<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" />
Ответ 2
У вас есть два варианта: один через другой код через 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 not checked."
android:textOn="The text for the component when it checked."
android:background="@drawable/offbuttonbg"
android:textAppearance="@style/SwitchTextAppearance"
android:thumb="@drawable/switchselector" />
Всегда обращайтесь к Switch Documentation, если вам нужна дополнительная информация.