禁用时更改开关颜色

Sha*_*ras 1 android material-design

我有一个开关,启用和检查时,它的颜色是我的colorPrimary.

我想在检查但是禁用时使用相同的颜色,我找不到完成它的方法.

我尝试使用选择器,但它改变了开关背景而不是切换本身.

如何更改开关颜色的切换?

谢谢.

Ami*_*r_P 11

1 - 在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>
Run Code Online (Sandbox Code Playgroud)

然后这为你的开关:

<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SwitchStyle" />
Run Code Online (Sandbox Code Playgroud)

2开关:

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector.xml" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)