Change switch color when disabled

倖福魔咒の 提交于 2021-02-11 07:37:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!