我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。

我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。

这应该很容易,但我似乎不知道该怎么做。


当前回答

派对来晚了,但我就是这么做的

风格

 <style name="SCBSwitch" parent="Theme.AppCompat.Light">
        <!-- active thumb & track color (30% transparency) -->
        <item name="colorControlActivated">#46bdbf</item>

        <!-- inactive thumb color -->
        <item name="colorSwitchThumbNormal">#f1f1f1
        </item>

        <!-- inactive track color (30% transparency) -->
        <item name="android:colorForeground">#42221f1f
        </item>
    </style>

颜色

布局

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:checked="false"
    android:theme="@style/SCBSwitch" />

结果

请参见颜色变化的启用和禁用开关

其他回答

在xml中,你可以这样改变颜色:

 <androidx.appcompat.widget.SwitchCompat
    android:id="@+id/notificationSwitch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    app:thumbTint="@color/darkBlue"
    app:trackTint="@color/colorGrey"/>

您可以动态更改为:

Switch.thumbDrawable.setColorFilter(ContextCompat.getColor(requireActivity(), R.color.darkBlue), PorterDuff.Mode.MULTIPLY)

试着找出正确的答案在这里:选择器的背景颜色的TextView。 简而言之,你应该在XML中创建带有颜色的Shape,然后在选择器中将其赋值为“checked”状态。

在Android Lollipop和以上版本中,在你的主题样式中定义它:

<style name="BaseAppTheme" parent="Material.Theme">
    ...
    <item name="android:colorControlActivated">@color/color_switch</item>
</style>

Android 2022 -最简单直接的方法:

的变化

/res/values/themes.xml

FROM

<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>

TO

<!-- Secondary brand color. -->
<item name="colorSecondary">@color/purple_500</item>
<item name="colorSecondaryVariant">@color/purple_700</item>

要在不使用style. XML或Java代码的情况下更改Switch样式,您可以将Switch定制为布局XML:

<Switch
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:thumbTint="@color/blue"
        android:trackTint="@color/white"
        android:checked="true"
        android:layout_height="wrap_content" />

它的属性android:thumbTint和android:trackTint,允许你自定义颜色

下面是这个XML的可视化结果: