【问题标题】:Change switch color when disabled禁用时更改开关颜色
【发布时间】:2016-07-25 12:34:09
【问题描述】:

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

我希望在选中但禁用时具有相同的颜色,但我找不到完成它的方法。

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

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

谢谢。

【问题讨论】:

    标签: android material-design


    【解决方案1】:

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

    【讨论】:

    • 我已经更新了我的帖子重试第一个解决方案如果不起作用尝试第二个@Sharas
    • colorSwitchThumbNormal 无法正常工作
    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 2020-10-20
    • 2018-04-12
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    相关资源
    最近更新 更多