如何改变SwitchCompat的轨道颜色

我试过使用下面的链接来改变SwitchCompat的颜色:

如何改变一个SwitchCompat的颜色

注意我的开关中的低对比度:

SwitchCompat

但是,在更改所有相关的颜色值后,SwitchCompat的轨道(较亮的灰色)保持不变。 除了颜色,我不想改变外观。 拇指是粉红色的,我希望轨道有一些对比。 我想在我的styles.xml中定义一个值吗?

我试过这些值(随机的非白色):

<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> ...> 

我有同样的问题,并解决它。

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

我阅读应用程序兼容代码,并理解它。

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; } 

以下是AppCompat编程方式更改轨道和拇指颜色的方法,适用于特定的SwitchCompat 。 对于这个例子,我已经将thumbColor硬编码为红色。 理想情况下,您可以通过第二个方法参数设置颜色。

请注意,当检查开关时,会显示波纹。 以下代码不会改变波纹颜色。

 public static void setSwitchColor(SwitchCompat v) { // thumb color of your choice int thumbColor = Color.RED; // trackColor is the thumbColor with 30% transparency (77) int trackColor = Color.argb(77, Color.red(thumbColor), Color.green(thumbColor), Color.blue(thumbColor)); // setting the thumb color DrawableCompat.setTintList(v.getThumbDrawable(), new ColorStateList( new int[][]{ new int[]{android.R.attr.state_checked}, new int[]{} }, new int[]{ thumbColor, Color.WHITE })); // setting the track color DrawableCompat.setTintList(v.getTrackDrawable(), new ColorStateList( new int[][]{ new int[]{android.R.attr.state_checked}, new int[]{} }, new int[]{ trackColor, Color.parseColor("#4D000000") // full black with 30% transparency (4D) })); } 

如果您希望在一个Activity中使用多种颜色进行更多切换,则可以使用此解决方案(基于@Konifar的主题):

 <style name="CustomSwitchTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!-- Active thumb color & Active track color(30% transparency) --> <item name="colorControlActivated">@color/custom</item> <!-- Inactive thumb color --> <item name="colorSwitchThumbNormal">#E0E0E0</item> <!-- Inactive track color(30% transparency) --> <item name="android:colorForeground">#757575</item> </style> 

当开关被激活时@color/custom是拇指的颜色。

然后把这个主题应用到你的SwitchCompat中:

 <android.support.v7.widget.SwitchCompat android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/CustomSwitchTheme" /> 

我遇到了同样的问题。 最后用这个Kotlin代码以编程方式解决它

 fun tintSwitchButton(sw: SwitchCompat, resolvedColor: Int) { val states = arrayOf( intArrayOf(-android.R.attr.state_pressed), intArrayOf(android.R.attr.state_pressed) ) DrawableCompat.setTintList(sw?.trackDrawable, ColorStateList( states, intArrayOf(resolvedColor, resolvedColor) )) DrawableCompat.setTintList(sw?.thumbDrawable, ColorStateList( states, intArrayOf(Color.WHITE, Color.WHITE) )) } 

而函数调用是

 tintSwitchButton(switchCompat, Color.rgb(214, 0, 0)) 

你也可以创建一个扩展函数:

 fun SwitchCompat.tint(resolvedColor: Int) { val states = arrayOf( intArrayOf(-android.R.attr.state_pressed), intArrayOf(android.R.attr.state_pressed) ) DrawableCompat.setTintList(trackDrawable, ColorStateList( states, intArrayOf(resolvedColor, resolvedColor) )) DrawableCompat.setTintList(thumbDrawable, ColorStateList( states, intArrayOf(Color.WHITE, Color.WHITE) )) } 

所以电话会更容易

 switchCompat.tint(Color.rgb(214,0,0)) 
  Here is Switch Layout -Adding theme to your switch to change the color of track.Check the style given below:-. **Switch Compact** <android.support.v7.widget.SwitchCompat android:id="@+id/goOnlineBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" app:theme="@style/Switch_style/> **Switch_style** <style name="Switch_style" parent="Theme.AppCompat.Light"> <!-- active thumb & track color (30% transparency) --> <item name="colorControlNormal">@android:color/white</item> <item name="colorControlActivated">@android:color/blue</item> <item name="colorSwitchThumbNormal">@android:color/white</item> <item name="trackTint">@color/white</item> </style> 

trackTint会改变你的曲目颜色