如何更改Android Switch小部件的拇指的文本颜色(ON / OFF)?

时间:2015-03-20 00:29:53

标签: android android-widget

我尝试设置textAppearance属性。但它不起作用。还有另一个属性textColor,但它适用于交换机的标签。

    <Switch
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/double_margin"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textAppearance="@style/SwitchTextAppearance"
        android:track="@drawable/transit_switch_track"
        android:thumb="@drawable/transit_switch_thumb" />

<style name="SwitchTextAppearance">
    <item name="android:textColor">@android:color/red</item>
</style>

1 个答案:

答案 0 :(得分:1)

我通过使用drawable而不是试图摆弄颜色来解决这个问题。

所以这是代码:

    <Switch
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/double_margin"
        android:switchMinWidth="50dp"
        android:textOff=""
        android:textOn=""
        android:thumb="@drawable/switch_thumb"
        android:track="@drawable/switch_track"
        android:checked="false" />

switch_track.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/switch_off" android:state_checked="false"/>
    <item android:drawable="@drawable/switch_on" android:state_checked="true"/>

</selector>

switch_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/toggle" android:state_enabled="false"/>
    <item android:drawable="@drawable/toggle" android:state_pressed="true"/>
    <item android:drawable="@drawable/toggle" android:state_checked="true"/>
    <item android:drawable="@drawable/toggle"/>

</selector>

这就是我的Switch现在的样子:

enter image description here

Switch_off图像

switch_off image

Switch_on image

enter image description here

切换只是拇指的白色图像。