itch小部件textOn和textOff在Lollipop中不

itch小部件textOn和textOff在Lollipop中不

本文介绍了Android Switch小部件textOn和textOff在Lollipop中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Lollipop(5.0)中更改了开关小部件的行为。

The behavior of the switch widget changed in 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时呈现的开关:

Rendered switch when targetSdkVersion=19:

targetSdkVersion时呈现的开关= 21:

Rendered switch when targetSdkVersion=21:

请注意,Android Studio中的预览渲染仍会生成带有文本的开关,但是当在带有Lollipop(Nexus(Nexus)的设备上运行以targetSdkVersion = 21构建的apk时,该开关会丢失其文本5)。在同一Lollipop设备上运行由targetSdkVersion = 19构建的apk,可以正常显示带有预期文本的开关。

Note that preview rendering in Android Studio still produces a switch with text, but the switch loses it's text when an apk built with targetSdkVersion=21 is run on a device with Lollipop (Nexus 5). Running an apk built with targetSdkVersion=19 on the same Lollipop device renders the switch properly with text as expected.

为什么?有建议的解决方法吗?

Why? Any suggested workarounds?

推荐答案

默认情况下,材质主题下未显示文本,因为开关小部件资产无法很好地处理文本。您设置的任何文本都将用于描述无障碍服务的内容。

Text is not shown by default under Material theme since the switch widget assets don't work well with text. Any text that you do set will be used to describe the content to accessibility services.

您可以使用属性或方法。

You can change this using the android:showText property or Switch.setShowText(boolean) method.

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

如果使用AppCompat开关,请使用 app:showText

If you are using AppCompat switches, use app:showText instead.

这篇关于Android Switch小部件textOn和textOff在Lollipop中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:52