Android开关部件textOn和textOff在棒棒糖中不起作用

Lollipop(5.0)中切换小部件的行为发生了变化。

<Switch android:id="@+id/switcher" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginBottom="16dp" android:layout_marginRight="8dp" android:layout_marginEnd="8dp" android:layout_toEndOf="@id/another_view" android:layout_toRightOf="@id/another_view" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:textOff="@string/disabled" android:textOn="@string/enabled" android:fontFamily="sans-serif-condensed" /> 

当targetSdkVersion = 19时渲染开关:

在这里输入图像说明

当targetSdkVersion = 21时渲染开关:

在这里输入图像说明

请注意,Android Studio中的预览渲染仍然会产生一个带有文本的开关,但是在带有Lollipop(Nexus 5)的设备上运行带有targetSdkVersion = 21的apk的情况下,该开关会丢失文本。 在同一个棒棒糖设备上运行一个使用targetSdkVersion = 19构build的apk,可以按照预期正确地显示文本。

为什么? 任何build议的解决方法?

由于切换小部件资源不能很好地处理文本,文本在“材质”主题下默认不显示。 您所设置的任何文本都将用于描述无障碍服务的内容。

你可以使用android:showText属性或者Switch.setShowText(boolean)方法来改变它。

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

如果您使用的是AppCompat开关,请改用app:showText