我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。
我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。
这应该很容易,但我似乎不知道该怎么做。
我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。
我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。
这应该很容易,但我似乎不知道该怎么做。
当前回答
这是为我工作(需要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);
注意,需要最后添加“default”状态,如下所示。
我看到的唯一问题是,开关的“拇指”现在看起来比背景或开关的“轨道”更大。我认为这是因为我仍然在使用默认的轨道图像,它周围有一些空白空间。然而,当我尝试使用这种技术定制轨道图像时,我的开关似乎有一个1像素的高度,只有一段开/关文本出现。肯定有解决办法,但我还没找到……
Android 5更新
在Android 5中,上面的代码使开关完全消失。我们应该能够使用新的setButtonTintList方法,但对于开关,这似乎被忽略了。但这是可行的:
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.getThumbDrawable().setTintList(buttonStates);
switchInput.getTrackDrawable().setTintList(buttonStates);
更新Android 6-7
As Cheruby stated in the comments, we can use the new setThumbTintList and that worked as expected for me. We can also use setTrackTintList, but that applies the color as a blend, with a result that's darker than expected in dark color themes and lighter than expected in light color themes, sometimes to the point of being invisible. In Android 7, I was able to minimize that change that by overriding the track tint mode, but I couldn't get decent results from that in Android 6. You might need to define extra colors that compensate for the blending. (Do you ever get the feeling that Google doesn't want us to customize the appearance of our apps?)
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);
}
其他回答
从芳草中得到的解决方案对我很有效。 关于他的超空间问题,我解决了所有的填充到开关。
EDIT
按照要求,这是我的资料。
在布局文件中,我的开关是在一个线性布局和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 (min. Android 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)
你可以试试这个库,很容易改变颜色的开关按钮。 https://github.com/kyleduo/SwitchButton
作为现有答案的补充:您可以在res/color文件夹中使用选择器自定义拇指和跟踪,例如:
switch_track_selector
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/lightBlue"
android:state_checked="true" />
<item android:color="@color/grey"/>
</selector>
switch_thumb_selector
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/darkBlue"
android:state_checked="true" />
<item android:color="@color/white"/>
</selector>
使用这些选择器自定义轨道和拇指色调:
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:trackTint="@color/switch_track_selector"
app:thumbTint="@color/switch_thumb_selector"/>
请记住,如果你使用标准Switch和android命名空间的这些属性,它将只适用于API 23和以后,所以使用SwitchCompat与应用命名空间xmlns:app="http://schemas.android.com/apk/res-auto"作为通用解决方案。
结果:
要在不使用style. XML或Java代码的情况下更改Switch样式,您可以将Switch定制为布局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:thumbTint和android:trackTint,允许你自定义颜色
下面是这个XML的可视化结果:
如果你想通过编程改变开关组件颜色,请使用下面的代码:
binding.switchCompatBackupMedia.thumbTintList =
ColorStateList.valueOf(Color.parseColor("#00C4D3"))
binding.switchCompatBackupMedia.trackTintList =
ColorStateList.valueOf(Color.parseColor("#00C4D31F"))