2012-08-24 23 views

回答

79

爲交換機switch_layout.xml創建佈局。菜單自定義佈局應始終RelativeLayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <Switch 
     android:id="@+id/switchForActionBar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" /> 

</RelativeLayout> 

然後,在你mainmenu.xml添加的項目如下

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+id/myswitch" 
     android:title="" 
     android:showAsAction="always" 
     android:actionLayout="@layout/switch_layout" 
    /> 
</menu> 

而在你的活動,膨脹mainmenu.xml因爲你總是這樣

getMenuInflater().inflate(R.menu.mainmenu, menu); 
return true; 
+2

嘿「自定義佈局的菜單應該始終是RelativeLayout」。是否有任何文檔狀態? – Yeung

+0

嗯,這是真的。如果我更改爲FrameLayout,則開關不易觸摸。 – Yeung

+0

如果我使用LinearLayout,在這裏一切正常。謝謝你的回答! – Erwin

4

Ezequiel給出的解決方案非常棒,而且很有效。這裏去另一種方法:

定義您的自定義佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" > 
    <Switch 
     android:id="@+id/actionbar_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" /> 
</RelativeLayout> 

它充氣編程:

ActionBar actionBar = getSupportActionBar(); 
actionBar.setCustomView(R.layout.actionbar_top); 
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM); 
... 
Switch button = (Switch) findViewById(R.id.actionbar_switch); 
+1

哇...這解決了我的問題!我似乎無法使用Ezequiel的解決方案,但這對我來說是個解決方案!也許它與我用作ActionBar的新工具欄有關?無論如何,謝謝!注意:這似乎與我原來的方法不同,我希望它可以作爲菜單的自定義視圖...但有沒有辦法讓這個自定義視圖對齊到操作欄的右側? –

+0

找出並分享同一船上的人的答案。 –

+0

@VuNguyen太棒了!我仍然處理它,因爲我有一個片段內的這個ActionBar,findViewById返回null,將解決時更新:D –

27

終於想通了,我的問題:對於那些目前正使用新的程序兼容性,你應該使用android.support.v7.widget.SwitchCompat而不是Switch在交換機佈局上......否則,它不會顯示在ActionBar(假設你也使用AppCompat ActionBar),那麼actionLayout屬性不起作用,它必須設置爲t他編碼。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/switchView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/switchForActionBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="" /> 

</RelativeLayout> 

然後在代碼中設置的佈局:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    MenuItem item = menu.findItem(R.id.on_off_switch); 
    item.setActionView(R.layout.on_off_switch); 
    return true; 
} 
+1

使用'SwitchCompat'還有另外一個優點:交換機以更新的外觀(例如材料設計)顯示。 – DenisGL

+13

爲什麼每個人在RelativeLayout上都使用'android:orientation =「horizo​​ntal」'? – zackygaurav

+0

@zackygaurav作爲我的測試,它不需要使用'RelativeLayout',我正在使用'LinearLayout'並且它完美地工作。 – drakeet

25

如果物件沒有在行動吧很可能是因爲您在使用程序兼容性爲您的操作欄出現。爲了解決這個開關「機器人」來「應用程序:」在「showAsAction」和「actionLayout」的前面,你menu.xml文件

添加物品到XML,與應用:在地方的android:

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item 
       android:id="@+id/myswitch" 
       android:title="" 
       app:showAsAction="always" 
       app:actionLayout="@layout/switch_layout" 
      /> 
     </menu> 
您正在使用您的

進行佈局 「應用程序:actionLayout」
switch_layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 
    <Switch 
     android:id="@+id/switchAB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     /> 
</RelativeLayout> 

充氣菜單中你ActionBarActivity你通常會

getMenuInflater().inflate(R.menu.mainmenu, menu); 
    return true; 

這應該使開關出現在您的動作欄中,如果它沒有出現。

+0

這是一個微妙但很好的簡化/以上答案的補充! – swooby

+1

像這樣的小細節很容易監督和忘記調整。我總是花幾個小時試圖弄清楚是錯的。現在,如果菜單中沒有出現,我有一個經驗法則來檢查命名空間。 – ChallengeAccepted

+0

我知道它已經很長,但這個答案對我來說,現在的問題是我如何得到開關,以便我可以附加一個監聽程序編程? –

相關問題