禁用时更改开关颜色

5

我有一个开关,当它被启用并选中时,它的颜色是我的colorPrimary。

我希望在它被选中但禁用时也有相同的颜色,但我找不到方法实现它。

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

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

谢谢。

1个回答

15

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>

然后为您的交换机添加以下内容:

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

选择器.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>

我已更新了我的帖子,请再尝试第一种解决方案,如果不行,请尝试第二种。@Sharas - Amir_P
colorSwitchThumbNormal在正常状态下无法工作。 - jairhumberto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接