我怎样才能格调Android的切换?格调、Android

2023-09-11 12:02:32 作者:两分凉薄

在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 samsungs touchwiz theme

我以为生病需要一些状态可绘,以及香港专业教育学院看到了一些样式http://developer.android.com/reference/android/R.styleable.html与Switch_thumb和Switch_track看起来像什么,我可能会后,我只是不知道如何去使用它们。

i assume ill need some state drawables, and ive 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 dont know how to go about using them.

使用ActionbarSherlock林如果有差别,但我明白,只有设备上运行的API V14或以上将能够使用一个开关可言,所以这纯粹是为了那些设备。

Im using ActionbarSherlock if that makes a difference, though i understand that only devices running API v14 or above will be able to use a switch at all, So this is purely for those devices.

推荐答案

您可以定义用于后台可绘制和切换器的部分​​是这样的:

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:

的失活版本(即机器人使用xhdpi版本) 在pressed滑块: 激活的滑块(状态): 默认版本(关闭状态):

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.