[英]How can I change the Text color of the thumb(ON/OFF) for Android Switch widget?
我尝试设置textAppearanceAttribute。 但这是行不通的。 还有另一个属性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>
我通过使用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现在的外观:
开关图片
开机图片
切换只是拇指的白色图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.