我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。
我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。
这应该很容易,但我似乎不知道该怎么做。
我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。
我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。
这应该很容易,但我似乎不知道该怎么做。
当前回答
您可以为开关小部件自定义样式 在自定义样式时使用颜色重音作为默认值
<style name="switchStyle" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item> <!-- set your color -->
</style>
其他回答
到目前为止,最好从AppCompat中使用SwitchCompat。v7图书馆。然后,您可以使用简单的样式来更改组件的颜色。
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">
可能有点晚了,但是对于开关按钮,toogle按钮不是答案,你必须改变开关xml参数中的drawable:
android:thumb="your drawable here"
派对来晚了,但我就是这么做的
风格
<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" />
结果
请参见颜色变化的启用和禁用开关
您可以为开关小部件自定义样式 在自定义样式时使用颜色重音作为默认值
<style name="switchStyle" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item> <!-- set your color -->
</style>
你可以试试这个库,很容易改变颜色的开关按钮。 https://github.com/kyleduo/SwitchButton