改变开关的“开”颜色

新手上路,请多包涵

我在 ICS 应用程序中使用带有 holo.light 主题的标准 Switch 控件。

我想将切换按钮的突出显示或打开状态颜色从标准浅蓝色更改为绿色。

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

原文由 JamesSugrue 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 584
2 个回答

到目前为止,最好使用 AppCompat.v7 库中的 SwitchCompat。然后,您可以使用简单的样式来更改组件的颜色。

 values/themes.xml:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_awesome_color</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/my_awesome_darker_color</item>

    <!-- colorAccent is used as the default value for colorControlActivated,
         which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>

    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight, and colorSwitchThumbNormal. -->

</style>

参考: Android 开发者博客

编辑

它应该正确应用的方式是通过 android:theme="@style/Theme.MyTheme" 这也可以应用于父样式,例如 EditTexts、RadioButtons、Switches、CheckBoxes 和 ProgressBars:

 <style name="My.Widget.ProgressBar" parent="Widget.AppCompat.ProgressBar">

<style name="My.Widget.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">

原文由 SubChord 发布,翻译遵循 CC BY-SA 4.0 许可协议

派对迟到了,但我就是这样做的

风格

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

结果

查看启用和禁用开关的颜色变化

在此处输入图像描述

原文由 Hitesh Sahu 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题