问题描述
API 14 中引入的开关小部件的样式默认为全息主题.我想稍微改变它的风格,出于品牌原因稍微改变它的颜色和形状.如何解决这个问题?我知道这一定是可能的,因为我看到了默认 ICS 和三星的 touchwiz 主题之间的区别
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
我想我需要一些状态可绘制对象,并且我在 http 中看到了一些样式://developer.android.com/reference/android/R.styleable.html 带有 Switch_thumb 和 Switch_track 看起来就像我想要的那样.我只是不知道如何使用它们.
I assume I'll need some state drawables, and I've seen a few styles in http://developer.android.com/reference/android/R.styleable.html with Switch_thumb and Switch_track that look like what I might be after. I just don't know how to go about using them.
我正在使用 ActionbarSherlock,如果这有区别的话.当然,只有运行 API v14 或更高版本的设备才能使用 switch.
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.
推荐答案
您可以像这样定义用于背景的可绘制对象和切换器部分:
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" />
现在您需要创建一个选择器来定义切换器可绘制的不同状态.以下是 Android 来源的副本:
Now you need to create a selector that defines the different states for the switcher drawable.Here the copies from the Android sources:
<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>
这定义了可绘制的拇指,即在背景上移动的图像.有四个 ninepatch 图像用于滑块:
This defines the thumb drawable, the image that is moved over the background. There are four ninepatch images used for the slider:
停用版本(Android 正在使用的 xhdpi 版本)
按下的滑块:
激活的滑块(开启状态):
默认版本(关闭状态):
The deactivated version (xhdpi version that Android is using)
The pressed slider:
The activated slider (on state):
The default version (off state):
在以下选择器中还定义了三种不同的背景状态:
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 focused version:
And 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.
这篇关于如何为 Android Switch 设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!