我想在我的佈局使用Android 4.0風格的切換按鈕。爲此,我選擇了Theme = Theme.Holo.Light。如果按鈕被啓用,當我從那裏拿着那個綠色線條的方形按鈕時。
但我想使用他們在那裏配置頂部(看看在打印屏幕)的togglebutton。
問題
我如何使用thise切換按鈕?一些Codesnippets或快速教程將是偉大的!
問候
野生動物園
圖片
我想在我的佈局使用Android 4.0風格的切換按鈕。爲此,我選擇了Theme = Theme.Holo.Light。如果按鈕被啓用,當我從那裏拿着那個綠色線條的方形按鈕時。
但我想使用他們在那裏配置頂部(看看在打印屏幕)的togglebutton。
問題
我如何使用thise切換按鈕?一些Codesnippets或快速教程將是偉大的!
問候
野生動物園
圖片
新編輯:我現在做的切換回API等級8的全向移植並投放在GitHub上: https://github.com/ankri/SwitchCompatLibrary
舊後與我的交換機的自定義實現:
我對晚會有點遲,但我遇到了同樣的問題。我從另一個帖子中取得源代碼並製作了我自己的版本。
你可以找到源代碼和文檔on my website
這是什麼樣子:
編輯:更新的鏈接和圖片
更新:新的圖像在兩個工作光和黑暗的背景。原始圖像仍然可用。
此外,正如有人在評論中指出的那樣,請務必將它們保存爲「* .9」。PNG」,即‘switch_on_on_db.9.png’等
Ankri的答案是偉大的,但小有一點沉重。此外,他使用,而不是舊的(在我看來4.2的風格切換,更漂亮。)4.1風格的按鈕快速修復,我做了一個繪製,這樣你可以風格的切換按鈕,看起來像一個開關
首先,這裏是按鈕樣式:
<?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>
其中提到這些圖片:
從這裏下載原始圖像:
最後,你可以風格切換按鈕,如下所示:
<ToggleButton
android:id="@+id/ParamToggleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/toggletoswitch"
android:textOff=""
android:textOn=""/>
UPDATE:
個果凍豆版本(雖然不完全相同)現已:以上
偉大的工作!真的很感謝你! – safari 2013-03-26 19:21:39
非常感謝! :) – 2014-03-10 09:42:44
真棒帖子! – 2014-04-21 21:11:12
的最佳解決方案...謝謝! (沒有名字給?) 我想有人可能能夠使用我的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>
@繪製/切換按鈕是指上述的選擇。再次感謝!
您可以創建切換按鈕的自定義佈局。 – Mayank 2012-03-29 07:19:38
所以這是一個cutomlayout或什麼? – safari 2012-03-29 07:37:44