如何更改 SwitchCompat 的轨道颜色 [英] How to change the track color of a SwitchCompat
问题描述
我尝试使用以下链接更改 SwitchCompat 的颜色:
I've tried using the following link to change the color of a SwitchCompat:
注意我的开关中的低对比度:
Notice the low constrast in my switch:
但是在更改所有相关颜色值后,SwitchCompat 的轨道(更亮的灰色)保持不变.除了颜色,我不想改变外观.拇指是粉红色的,我希望曲目有一些对比.我是否错过了在我的 style.xml 中定义一个值?
But after changing all relevant color values the track (the brighter grey) of the SwitchCompat remains the same. I don't want to change the appearance except the color. The thumb is in pink, and I want the track to have some contrast. Did I miss to define a value in my styles.xml?
我尝试了这些值(随机的非白色):
I tried these values (random non-white colors):
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/first</item>
<item name="colorPrimaryDark">@color/second</item>
<item name="colorAccent">@color/third</item>
...
<item name="colorControlActivated">@color/first</item>
<item name="colorControlHighlight">@color/first</item>
<item name="colorControlNormal">@color/second</item>
<item name="colorSwitchThumbNormal">@color/second</item>
<item name="colorButtonNormal">@color/second</item>
...>
推荐答案
我遇到了同样的问题并解决了.
I had same probrem and solved it.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<!-- Active thumb color & Active track color(30% transparency) -->
<item name="colorControlActivated">@color/theme</item>
<!-- Inactive thumb color -->
<item name="colorSwitchThumbNormal">@color/grey300</item>
<!-- Inactive track color(30% transparency) -->
<item name="android:colorForeground">@color/grey600</item>
...
</style>
我阅读了应用兼容代码,并理解了它.
I read app compat code, and understand it.
android.support.v7.internal.widget.TintManager.java
private ColorStateList getSwitchTrackColorStateList() {
if (mSwitchTrackStateList == null) {
final int[][] states = new int[3][];
final int[] colors = new int[3];
int i = 0;
// Disabled state
states[i] = new int[] { -android.R.attr.state_enabled };
colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.1f);
i++;
states[i] = new int[] { android.R.attr.state_checked };
colors[i] = getThemeAttrColor(R.attr.colorControlActivated, 0.3f);
i++;
// Default enabled state
states[i] = new int[0];
colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.3f);
i++;
mSwitchTrackStateList = new ColorStateList(states, colors);
}
return mSwitchTrackStateList;
}
这篇关于如何更改 SwitchCompat 的轨道颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!