我有一个开关,当它被启用并选中时,它的颜色是我的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:
<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>