1. 程式人生 > >Android switch在某些系統上不顯示thumb和track的解決辦法

Android switch在某些系統上不顯示thumb和track的解決辦法

在某些版本的系統上,thumb顯示不出來,自定義的track也顯示出來,
1、檢視原始碼後得知,在某些android系統版本上,thumb的寬度是和textOn,textOff的文字寬度是一致的,是不會重新計算thumb的drawable的寬度的。為了相容這些系統(比如4.4.2)可以在textOn中設定一些空格,點據一定的寬度,這樣thumb就有一定的寬度可以顯示出來了。

<Switch
        android:id="@+id/message_danmu_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
android:layout_weight="0" android:clickable="true" android:selectAllOnFocus="true" android:showText="false" android:switchTextAppearance="@android:style/TextAppearance.Small" android:textOff="&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;"
android:textOn="&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;" android:textSize="16sp" android:thumb="@drawable/icon_barrage" android:thumbTextPadding="0dp" android:track="@drawable/my_switch_track"/>

2、track顯示不出來,經過測試,給track中的drawable設定一下寬度就可以了

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#6FD764"/>

    <corners
        android:radius="5dp"/>

    <size
        android:width="30dp"
        android:height="34dp"/>

</shape>