2014-12-03 21 views
40

Lollipop(5.0)中切換部件的行爲發生了變化。Android開關部件textOn和textOff在棒棒糖中不起作用

<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

渲染開關:

enter image description here

渲染開關時targetSdkVersion = 21:

enter image description here

注意,預覽Android Studio中呈現仍然產生一個開關帶有文本,但是當用targetSdkVersion = 21構建的apk是ru時,開關就會丟失文本在具有棒棒糖(Nexus 5)的設備上。按照預期在相同的棒棒糖設備上運行使用targetSdkVersion = 19構建的apk文本,可以正確顯示交換機。

爲什麼?任何建議的解決方法?

回答

98

由於切換小部件資源不適用於文本,文本在默認情況下不會顯示在Material主題下。您設置的任何文本都將用於描述無障礙服務的內容。

您可以使用android:showText屬性或Switch.setShowText(boolean)方法更改此設置。

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

如果您使用的是AppCompat開關,請改爲使用app:showText

+0

謝謝,這對我也有幫助。 – Kitalda 2015-05-12 08:53:54

+0

該屬性確實是從API級別21開始支持的。 – manfcas 2016-04-07 16:45:06

+0

似乎無法再使用N-Preview3設備上的23.4.0運行了。 編輯:應該是'app:showText =「true」' – behelit 2016-05-26 06:38:48