【发布时间】:2016-07-25 12:34:09
【问题描述】:
我有一个开关,当启用并选中时,它的颜色就是我的 colorPrimary。
我希望在选中但禁用时具有相同的颜色,但我找不到完成它的方法。
我尝试使用选择器,但它会更改开关背景,而不是切换开关本身。
如何更改开关颜色的切换?
谢谢。
【问题讨论】:
我有一个开关,当启用并选中时,它的颜色就是我的 colorPrimary。
我希望在选中但禁用时具有相同的颜色,但我找不到完成它的方法。
我尝试使用选择器,但它会更改开关背景,而不是切换开关本身。
如何更改开关颜色的切换?
谢谢。
【问题讨论】:
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>
【讨论】: