改变开关的“开”颜色

我在一个ICS应用程序中使用了一个带有holo.light主题的标准开关控件。

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

这应该很容易,但我似乎无法解决如何做到这一点。

截至目前,最好使用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开发人员博客

晚会派对,但这是我做的

样式

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

结果

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

在这里输入图像说明

这是为我工作(需要Android 4.1):

 Switch switchInput = new Switch(this); int colorOn = 0xFF323E46; int colorOff = 0xFF666666; int colorDisabled = 0xFF333333; StateListDrawable thumbStates = new StateListDrawable(); thumbStates.addState(new int[]{android.R.attr.state_checked}, new ColorDrawable(colorOn)); thumbStates.addState(new int[]{-android.R.attr.state_enabled}, new ColorDrawable(colorDisabled)); thumbStates.addState(new int[]{}, new ColorDrawable(colorOff)); // this one has to come last switchInput.setThumbDrawable(thumbStates); 

请注意,“默认”状态需要最后添加,如下所示。

我所看到的唯一问题是交换机的“拇指”现在看起来比交换机的背景或“轨道”大。 我认为这是因为我仍然使用默认的轨道图像,它周围有一些空的空间。 但是,当我尝试使用这种技术来定制轨道图像时,我的开关似乎具有1像素的高度,仅出现了开/关文本的条子。 必须有一个解决scheme,但我还没有find它…

Android 5.1更新

在Android 5.1中,上面的代码使交换机完全消失。 我们应该可以使用新的setButtonTintList方法,但是当我尝试它时,这没有任何效果。 我现在正在使用这个代码:

 switchInput.getThumbDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY); switchInput.getTrackDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY); 

唯一的缺点是没有办法设置禁用状态的颜色。 轨道变得更褪色,但拇指仍然是相同的颜色,所以变化比我想要的更微妙。

Android 5.1.1更新

显然,setButtonTintList()方法在5.1.1中已经修复了一个bug。 此代码现在可以按预期工作,没有任何缺陷,我可以看到:

 ColorStateList buttonStates = new ColorStateList( new int[][]{ new int[]{-android.R.attr.state_enabled}, new int[]{android.R.attr.state_checked}, new int[]{} }, new int[]{ Color.BLUE, Color.RED, Color.GREEN } ); switchInput.setButtonTintList(buttonStates); 

Android 6-7更新

在Android 6-7中, setButtonTintList在交换机上被忽略。 正如Cheruby在评论中所述,我们可以使用新的setThumbTintList ,并按照我的预期工作。 我们也可以使用setTrackTintList ,但将颜色作为混合使用,结果在深色主题中比预期更暗,在浅色主题中比预期浅,有时甚至是不可见的。 在Android 7中,我可以通过覆盖轨道着色模式来最小化这种变化,但是我无法从Android 6中获得体面的结果。您可能需要定义补充混合的额外颜色。 (你有没有觉得谷歌不希望我们定制我们的应用程序的外观?)

 ColorStateList thumbStates = new ColorStateList( new int[][]{ new int[]{-android.R.attr.state_enabled}, new int[]{android.R.attr.state_checked}, new int[]{} }, new int[]{ Color.BLUE, Color.RED, Color.GREEN } ); switchInput.setThumbTintList(thumbStates); if (Build.VERSION.SDK_INT >= 24) { ColorStateList trackStates = new ColorStateList( new int[][]{ new int[]{-android.R.attr.state_enabled}, new int[]{} }, new int[]{ Color.GRAY, Color.LTGRAY } ); switchInput.setTrackTintList(trackStates); switchInput.setTrackTintMode(PorterDuff.Mode.OVERLAY); } 

创build一个自定义的开关并覆盖setChecked来改变颜色:

  public class SwitchPlus extends Switch { public SwitchPlus(Context context) { super(context); } public SwitchPlus(Context context, AttributeSet attrs) { super(context, attrs); } public SwitchPlus(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void setChecked(boolean checked) { super.setChecked(checked); changeColor(checked); } private void changeColor(boolean isChecked) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { int thumbColor; int trackColor; if(isChecked) { thumbColor = Color.argb(255, 253, 153, 0); trackColor = thumbColor; } else { thumbColor = Color.argb(255, 236, 236, 236); trackColor = Color.argb(255, 0, 0, 0); } try { getThumbDrawable().setColorFilter(thumbColor, PorterDuff.Mode.MULTIPLY); getTrackDrawable().setColorFilter(trackColor, PorterDuff.Mode.MULTIPLY); } catch (NullPointerException e) { e.printStackTrace(); } } } } 

我改变了状态改变时更新颜色filter解决了它…

 public void bind(DetailItem item) { switchColor(item.toggle); listSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switchColor(b); } }); } private void switchColor(boolean checked) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { listSwitch.getThumbDrawable().setColorFilter(checked ? Color.BLACK : Color.WHITE, PorterDuff.Mode.MULTIPLY); listSwitch.getTrackDrawable().setColorFilter(!checked ? Color.BLACK : Color.WHITE, PorterDuff.Mode.MULTIPLY); } } 

可能会有点晚,但对于切换button,button不是答案,您必须更改开关的xml参数中的drawable:

  android:thumb="your drawable here" 

虽然SubChord的回答是正确的,但是并没有真正回答如何在不影响其他小部件的情况下设置“on”颜色的问题。 为此,在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" ... /> 

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

创build您自己的9贴片图像,并将其设置为切换button的背景。

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

要不使用style.xml或Java代码来更改切换样式,可以将切换器自定义为布局XML:

 <Switch android:id="@+id/checkbox" android:layout_width="wrap_content" android:thumbTint="@color/blue" android:trackTint="@color/white" android:checked="true" android:layout_height="wrap_content" /> 

它的属性android:thumbTintandroid:trackTint允许你自定义颜色

这是这个XML的可视化结果:

在这里输入图像说明

在Android棒棒糖及以上版本中,以您的主题风格定义它:

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

从arlomediabuild议的解决scheme为我工作。 关于他的extraspace的问题,我解决了删除交换机的所有填充。

编辑

按照要求,这里是我的。

在布局文件中,我的开关在一个线性布局内,在一个TextView之后。

 <LinearLayout android:id="@+id/myLinearLayout" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal|center" android:gravity="right" android:padding="10dp" android:layout_marginTop="0dp" android:background="@drawable/bkg_myLinearLayout" android:layout_marginBottom="0dp"> <TextView android:id="@+id/myTextForTheSwitch" android:layout_height="wrap_content" android:text="@string/TextForTheSwitch" android:textSize="18sp" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal|center" android:gravity="right" android:layout_width="wrap_content" android:paddingRight="20dp" android:textColor="@color/text_white" /> <Switch android:id="@+id/mySwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="@string/On" android:textOff="@string/Off" android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal" android:layout_toRightOf="@id/myTextForTheSwitch" android:layout_alignBaseline="@id/myTextForTheSwitch" android:gravity="right" /> </LinearLayout> 

由于我正在使用Xamarin / Monodroid(至lessAndroid 4.1),我的代码是:

 Android.Graphics.Color colorOn = Android.Graphics.Color.Green; Android.Graphics.Color colorOff = Android.Graphics.Color.Gray; Android.Graphics.Color colorDisabled = Android.Graphics.Color.Green; StateListDrawable drawable = new StateListDrawable(); drawable.AddState(new int[] { Android.Resource.Attribute.StateChecked }, new ColorDrawable(colorOn)); drawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(colorDisabled)); drawable.AddState(new int[] { }, new ColorDrawable(colorOff)); swtch_EnableEdit.ThumbDrawable = drawable; 

swtch_EnableEdit先前是这样定义的(Xamarin):

 Switch swtch_EnableEdit = view.FindViewById<Switch>(Resource.Id.mySwitch); 

我没有设置所有的填充,我不叫.setPadding(0,0,0,0)。

我不知道如何从Java中做到这一点,但如果你有一个为你的应用程序定义的风格,你可以在你的风格添加这一行,你会有我想要的颜色我已经使用#3F51B5

 <color name="ascentColor">#3F51B5</color> 

尝试找出正确的答案在这里: select器TextView的背景颜色 。 用两个词来说,你应该用颜色创buildXMLforms,然后在select器中将它赋值为“checked”。

您可以为切换小部件制作自定义样式,使用颜色口音作为默认样式

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