问题
I've a switch that when is enabled and checked, it's color is my colorPrimary.
I want to have the same color when it's checked but disabled, and i can't find a way to get it done.
I tried using a selector, but it changes the switch background and not the toggle itself.
How can i change the toggle of the switch color?
Thanks.
回答1:
1-use this in styles.xml
<style name="SwitchStyle" parent="Theme.AppCompat.Light">
<item name="colorControlActivated">@color/theme</item>
<item name="colorSwitchThumbNormal">@color/grey300</item>
<item name="android:colorForeground">@color/grey600</item>
</style>
and then this for your switch:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SwitchStyle" />
2-Switch:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector.xml" />
selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/on" android:state_checked="true"/>
<item android:drawable="@drawable/off" android:state_checked="false"/>
</selector>
来源:https://stackoverflow.com/questions/36428557/change-switch-color-when-disabled