2016-04-05 49 views
-3

我有一个开关,当启用并检查时,它的颜色是我的colorPrimary。禁用时更改开关颜色

我希望在检查但禁用时具有相同的颜色,但我找不到一种方法来完成它。

我试过使用选择器,但它改变了开关背景而不是开关本身。

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

谢谢。

回答

3

在styles.xml 1 - 使用此

<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> 

,然后这为交换机:

<android.support.v7.widget.SwitchCompat 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:theme="@style/SwitchStyle" /> 

2开关:

<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> 
+0

谢谢,但它不工作... – Sharas

+0

我已更新我的帖子再次尝试第一个解决方案,如果不工作尝试第二个@Sharas –