使用Android 4.0样式切换按钮

时间:2012-03-29 07:16:25

标签: android android-layout layout togglebutton

我正在尝试做什么


我正在尝试在我的布局中使用Android 4.0样式的togglebutton。为此,我选择了Theme = Theme.Holo.Light。当我从那里拿到togglebutton时,如果按钮被启用,则该绿色线的正方形。

但是我想使用togglebutton就像他们在那里配置顶部(看看打印屏幕)。

问题


我怎样才能使用这个togglebutton?一些Codesnippets或快速教程会很棒!

最好的问候

狩猎

图片


screenshot of the togglebuttons

4 个答案:

答案 0 :(得分:24)

新编辑:我现在完成了Switch的完整backport回到API Level 8并放入github: https://github.com/ankri/SwitchCompatLibrary


使用我自定义的Switch实现的旧帖子:

我有点迟到了,但我遇到了同样的问题。我从这篇文章中的另一篇文章中获取了源代码,并制作了我自己的版本。

您可以找到源代码和文档on my website

这就是它的样子:

SwitchButtonDemo screenshot

编辑:更新了链接和图片

答案 1 :(得分: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 here enter image description here

从这里下载原始图片:

Old Off

Old On

最后,您可以像这样设置togglebutton样式:

<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 here enter image description here

Old Off

Old On

答案 2 :(得分:6)

如果您的应用针对api级别14或更高级别。使用Switch小部件并确保您的应用程序的主题是“Theme.Holo”或“Theme.Holo.Light”

但是,如果要将api级别定位到2.3以下,则必须进行自定义布局。 我认为解释这个问题非常麻烦,我会给你一个例子。

您可以在here中找到“切换”按钮的真实实现。

嗯,你可以获得这个来源并投入你的项目。你会遇到一些错误,但解决它并不困难。

答案 3 :(得分:2)

上面有很好的解决方案......谢谢! (没有给出名字?) 我以为有人可能会使用我的xml,让togglebutton看起来像一个开关:

        <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指的是上述选择器。再次感谢!

相关问题