禁用时更改开关颜色 [英] Change switch color when disabled

查看:79
本文介绍了禁用时更改开关颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

谢谢.

推荐答案

1-在styles.xml中使用它

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>

然后是您的切换器

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

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>

这篇关于禁用时更改开关颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆