2012-03-29 56 views
14

我試圖做使用Android 4.0的風格切換按鈕


我想在我的佈局使用Android 4.0風格的切換按鈕。爲此,我選擇了Theme = Theme.Holo.Light。如果按鈕被啓用,當我從那裏拿着那個綠色線條的方形按鈕時。

但我想使用他們在那裏配置頂部(看看在打印屏幕)的togglebutton。

問題


我如何使用thise切換按鈕?一些Codesnippets或快速教程將是偉大的!

問候

野生動物園

圖片


screenshot of the togglebuttons

+0

您可以創建切換按鈕的自定義佈局。 – Mayank 2012-03-29 07:19:38

+0

所以這是一個cutomlayout或什麼? – safari 2012-03-29 07:37:44

回答

23

新編輯:我現在做的切換回API等級8的全向移植並投放在GitHub上: https://github.com/ankri/SwitchCompatLibrary


舊後與我的交換機的自定義實現:

我對晚會有點遲,但我遇到了同樣的問題。我從另一個帖子中取得源代碼並製作了我自己的版本。

你可以找到源代碼和文檔on my website

這是什麼樣子:

SwitchButtonDemo screenshot

編輯:更新的鏈接和圖片

+1

hey anri,超級arbeit hast da geleistet! Echt respekt! Gruss aus der Schweiz! – safari 2012-07-30 17:19:18

+0

他是非常有用的,但我有一個問題:我如何可以禁用開關。 setEnabled(false)不起作用。謝謝 – Paul 2012-11-08 15:08:10

+1

無法讓它與父= android一起工作:Theme.Wallpaper – 2013-12-30 14:14:06

6

如果您的應用定位API級別14或更高。使用Switch小部件,並確保您的應用程序的主題是「Theme.Holo」或「Theme.Holo.Light」

但是,如果您要定位2.3以下的api級別,則必須進行自定義佈局。 我覺得這很難解釋,我給你舉個例子。

你可以在here找到「開關」按鈕的真正實現。

那麼,你可以得到這個來源,並把你的項目。你會有一些錯誤,但解決它並不難。

+0

謝謝,我會看看它。 – safari 2012-03-29 07:54:59

+1

您是否設法使其工作?你能和我們分享你的解決方案嗎?謝謝 – Paul 2012-11-08 15:18:04

+0

實際上,最簡單的解決方案是使用一個切換按鈕,看起來像一個開關。我稍後會發布解決方案。編輯:添加解決方案。 – dberm22 2013-03-26 16:51:18

21

更新:新的圖像在兩個工作光和黑暗的背景。原始圖像仍然可用。

此外,正如有人在評論中指出的那樣,請務必將它們保存爲「* .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> 

其中提到這些圖片:

enter image description hereenter image description here

從這裏下載原始圖像:

Old Off

Old On

最後,你可以風格切換按鈕,如下所示:

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

UPDATE:

果凍豆版本(雖然不完全相同)現已:以上

enter image description hereenter image description here

Old Off

Old On

+0

偉大的工作!真的很感謝你! – safari 2013-03-26 19:21:39

+0

非常感謝! :) – 2014-03-10 09:42:44

+0

真棒帖子! – 2014-04-21 21:11:12

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> 

@繪製/切換按鈕是指上述的選擇。再次感謝!