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

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

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


当前回答

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

其他回答

虽然SubChord的回答是正确的,但它并没有真正回答如何在不影响其他小部件的情况下设置“打开”颜色的问题。要做到这一点,在styles.xml中使用ThemeOverlay:

<style name="ToggleSwitchTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">@color/green_bright</item>
</style>

并在你的开关中引用它:

<android.support.v7.widget.SwitchCompat
  android:theme="@style/ToggleSwitchTheme" ... />

这样做只会影响你想要应用它的视图的颜色。

这招对我很管用——

1.values/styles.xml中的代码:

 <style name="SwitchTheme" parent="Theme.AppCompat.Light">
    <item name="android:colorControlActivated">#148E13</item>
</style>

2.在布局文件的交换机中添加以下代码行-:

android:theme="@style/SwitchTheme"

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

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

创建自己的9-patch图像,并将其设置为切换按钮的背景。

http://radleymarx.com/2011/simple-guide-to-9-patch/

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

风格

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

结果

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