如何为 Android Switch 设置样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10118050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:32:54  来源:igfitidea点击:

How can I style an Android Switch?

androidandroid-themestylingandroid-stylesandroid-switch

提问by Glenn.nz

The switch widget introduced in API 14 is styled by default with holo theme. I want to style it slightly different, changing its colors and shape a bit for branding reasons. How does one go about this? I know it must be possible, as ive seen the difference between default ICS and Samsung's touchwiz theme

API 14 中引入的开关小部件默认采用全息主题样式。我想稍微改变它的风格,出于品牌原因稍微改变它的颜色和形状。如何解决这个问题?我知道这一定是可能的,因为我看到了默认 ICS 和三星的 touchwiz 主题之间的区别

enter image description here

在此处输入图片说明

I assume I'll need some state drawables, and I've seen a few styles in http://developer.android.com/reference/android/R.styleable.htmlwith Switch_thumb and Switch_track that look like what I might be after. I just don't know how to go about using them.

我假设我需要一些状态可绘制对象,并且我在http://developer.android.com/reference/android/R.styleable.html 中看到了一些带有 Switch_thumb 和 Switch_track 的样式,它们看起来像我可能想要的. 我只是不知道如何使用它们。

I'm using ActionbarSherlock if that makes a difference. Only devices running API v14 or above will be able to use a switch at all, of course.

如果这有所作为,我正在使用 ActionbarSherlock。当然,只有运行 API v14 或更高版本的设备才能使用 switch。

回答by Janusz

You can define the drawables that are used for the background, and the switcher part like this:

您可以定义用于背景的可绘制对象,以及如下所示的切换器部分:

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:thumb="@drawable/switch_thumb"
    android:track="@drawable/switch_bg" />

Now you need to create a selector that defines the different states for the switcher drawable. Here the copies from the Android sources:

现在您需要创建一个选择器来定义切换器可绘制的不同状态。这是来自Android来源的副本:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/switch_thumb_disabled_holo_light" />
    <item android:state_pressed="true"  android:drawable="@drawable/switch_thumb_pressed_holo_light" />
    <item android:state_checked="true"  android:drawable="@drawable/switch_thumb_activated_holo_light" />
    <item                               android:drawable="@drawable/switch_thumb_holo_light" />
</selector>

This defines the thumb drawable, the image that is moved over the background. There are four ninepatchimages used for the slider:

这定义了可绘制的拇指,即在背景上移动的图像。有四个 用于滑块的Ninepatch图像:

The deactivated version (xhdpi version that Android is using)The deactivated version
The pressed slider: The pressed slider
The activated slider (on state):The activated slider
The default version (off state): enter image description here

停用版本(Android 使用的 xhdpi 版本)停用版本
按下滑块:按下的滑块
激活滑块(开启状态):激活的滑块
默认版本(关闭状态):在此处输入图片说明

There are also three different states for the background that are defined in the following selector:

在以下选择器中还定义了三种不同的背景状态:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled_holo_dark" />
    <item android:state_focused="true"  android:drawable="@drawable/switch_bg_focused_holo_dark" />
    <item                               android:drawable="@drawable/switch_bg_holo_dark" />
</selector>

The deactivated version: The deactivated version
The focused version: The focused version
And the default version:the default version

停用版本:停用版本
重点版本:重点版本
和默认版本:默认版本

To have a styled switch just create this two selectors, set them to your Switch View and then change the seven images to your desired style.

要创建样式开关,只需创建这两个选择器,将它们设置为您的开关视图,然后将七个图像更改为您想要的样式。

回答by kishu27

It's an awesome detailed reply by Janusz. But just for the sake of people who are coming to this page for answers, the easier way is at http://android-holo-colors.com/(dead link) linked from Android Asset Studio

这是 Janusz 的一个很棒的详细回复。但只是为了那些来到这个页面寻求答案的人,更简单的方法是从 Android Asset Studio 链接的http://android-holo-colors.com/(死链接)

A good description of all the tools are at AndroidOnRocks.com(site offline now)

AndroidOnRocks.com 上对所有工具都有很好的描述(网站现在离线)

However, I highly recommend everybody to read the reply from Janusz as it will make understanding clearer. Use the tool to do stuffs real quick

但是,我强烈建议大家阅读 Janusz 的回复,因为它会使理解更清晰。使用该工具真正快速地做事

回答by Arnav Rao

You can customize material styles by setting different color properties. For example custom application theme

您可以通过设置不同的颜色属性来自定义材质样式。例如自定义应用程序主题

<style name="CustomAppTheme" parent="Theme.AppCompat">
    <item name="android:textColorPrimaryDisableOnly">#00838f</item>
    <item name="colorAccent">#e91e63</item>
</style>

Custom switch theme

自定义切换主题

<style name="MySwitch" parent="@style/Widget.AppCompat.CompoundButton.Switch">
    <item name="android:textColorPrimaryDisableOnly">#b71c1c</item>
    <item name="android:colorControlActivated">#1b5e20</item>
    <item name="android:colorForeground">#f57f17</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat</item>
</style>

You can customize switch track and switch thumblike below image by defining xml drawables. For more information http://www.zoftino.com/android-switch-button-and-custom-switch-examples

您可以通过定义 xml drawable 来自定义切换轨道和切换拇指,如下图所示。有关更多信息http://www.zoftino.com/android-switch-button-and-custom-switch-examples

custom switch track and thumb

自定义切换轨道和拇指

回答by netpork

Alternative and much easier way is to use shapes instead of 9-patches. It is already explained here: https://stackoverflow.com/a/24725831/512011

另一种更简单的方法是使用形状而不是 9 个补丁。这里已经解释过了:https: //stackoverflow.com/a/24725831/512011