使用Android 4.0风格的切换按钮

14

我想做什么


我想在我的布局中使用Android 4.0风格的togglebutton。为此,我选择了Theme = Theme.Holo.Light主题。当我从那里取出togglebutton时,如果按钮启用,它是一个带有绿线的正方形。

但是我想像他们在顶部的配置中那样使用togglebutton(请查看截屏)。

问题


我如何使用这些togglebutton?一些代码片段或快速教程将是很棒的!

最好的问候

safari

图片


togglebuttons的截图


您可以为切换按钮创建自定义布局。 - Mayank
这是一个自定义布局吗? - safari
4个回答

24

新编辑:我现在将Switch的全部版本都回退到了API Level 8,并将其放在github上:

https://github.com/ankri/SwitchCompatLibrary

旧帖子及自定义Switch实现:

我有点晚来这个派对,但我遇到了同样的问题。我从该线程中的其他帖子中获取了源代码,并制作了自己版本的开关。

您可以在我的网站上找到源代码和文档:http://ankri.de/switch-button-for-android-2-3-gingerbread/

这就是它的外观:

SwitchButtonDemo screenshot

编辑:已更新链接和图片


1
嗨Anri,你做得真棒!真是太了不起了!来自瑞士的问候! - safari
这非常有用,但我有一个问题:如何使开关处于禁用状态。 setEnabled(false)无效。谢谢。 - Paul
1
无法使用parent=android:Theme.Wallpaper使其正常工作。 - A.J.

21

更新:新图像可以在明暗背景下使用。原始图像仍可用。

此外,正如评论中有人指出的那样,请确保将它们保存为“*.9.png”,即“switch_on_on_db.9.png”等。


Ankri的答案很好,但有些繁琐。此外,他使用4.2风格的开关,而不是我认为更漂亮的4.1风格按钮。为了快速解决问题,我制作了一个drawable,让您可以将togglebutton样式设置成开关样式。

首先,这是按钮样式:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_on_on_db" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/switch_on_on_db" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/switch_off_off_db" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/switch_off_off_db" android:state_checked="false" android:state_focused="false"/>
</selector>

这些指的是以下图片:

enter image description hereenter image description here

从此处下载原始图片:

旧关

旧开

最后,您可以按照以下方式为切换按钮添加样式:

<ToggleButton 
    android:id="@+id/ParamToggleButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/toggletoswitch"
    android:textOff=""
    android:textOn=""/>

更新:

现在有Jelly Bean版本可用(虽然不完全相同):

enter image description hereenter image description here

旧版关闭

旧版开启


请确保将图像保存为9-patch格式,即“switch_off.9.png”。 - Someone Somewhere

6
如果你的应用程序的目标api级别是14或更高。请使用Switch小部件,并确保你的应用程序主题是"Theme.Holo"或"Theme.Holo.Light"
然而,如果你想针对2.3以下的api级别,则必须制作自定义布局。 我认为解释这个有点混乱,我会给你一个例子。
你可以在这里找到"Switch"按钮的真正实现。
好吧,你可以获取那个源代码并将其放入你的项目中。你会遇到一些错误,但解决它并不是很难。

1
你成功让它工作了吗?能否与我们分享你的解决方案?谢谢。 - Paul
其实,最简单的解决方案是使用一个样式化为开关的ToggleButton。稍后我会发布一个解决方案。编辑:已添加解决方案。 - dberm22

2

很棒的解决方案...谢谢!(未提供姓名?) 我认为有些人可能会用到我的xml,让切换按钮看起来像开关:

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right|center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_switchToggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dip"
                android:layout_marginTop="0dip"
                android:text="@string/shake_to_add"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />

            <ToggleButton
                android:id="@+id/switchToggle"
                android:layout_width="75dp"
                android:layout_height="20dp"
                android:layout_margin="5dip"
                android:background="@drawable/togglebutton"
                android:textOff=""
                android:textOn="" />
       </LinearLayout>

@drawable/togglebutton 指的是上述描述的选择器。再次感谢!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接