我想添加一個類似於jellybean原生外觀的按鈕開關。 (在視圖頂部的藍色/灰色開關) 如何添加一個開關到android動作欄?
文檔顯示如何在那裏創建菜單或添加圖標,但不說明如何添加自定義元素。例如。一個開關。 http://developer.android.com/guide/topics/ui/actionbar.html
我想添加一個類似於jellybean原生外觀的按鈕開關。 (在視圖頂部的藍色/灰色開關) 如何添加一個開關到android動作欄?
文檔顯示如何在那裏創建菜單或添加圖標,但不說明如何添加自定義元素。例如。一個開關。 http://developer.android.com/guide/topics/ui/actionbar.html
爲交換機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;
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);
哇...這解決了我的問題!我似乎無法使用Ezequiel的解決方案,但這對我來說是個解決方案!也許它與我用作ActionBar的新工具欄有關?無論如何,謝謝!注意:這似乎與我原來的方法不同,我希望它可以作爲菜單的自定義視圖...但有沒有辦法讓這個自定義視圖對齊到操作欄的右側? –
找出並分享同一船上的人的答案。 –
@VuNguyen太棒了!我仍然處理它,因爲我有一個片段內的這個ActionBar,findViewById返回null,將解決時更新:D –
終於想通了,我的問題:對於那些目前正使用新的程序兼容性,你應該使用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;
}
使用'SwitchCompat'還有另外一個優點:交換機以更新的外觀(例如材料設計)顯示。 – DenisGL
爲什麼每個人在RelativeLayout上都使用'android:orientation =「horizontal」'? – zackygaurav
@zackygaurav作爲我的測試,它不需要使用'RelativeLayout',我正在使用'LinearLayout'並且它完美地工作。 – drakeet
如果物件沒有在行動吧很可能是因爲您在使用程序兼容性爲您的操作欄出現。爲了解決這個開關「機器人」來「應用程序:」在「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;
這應該使開關出現在您的動作欄中,如果它沒有出現。
這是一個微妙但很好的簡化/以上答案的補充! – swooby
像這樣的小細節很容易監督和忘記調整。我總是花幾個小時試圖弄清楚是錯的。現在,如果菜單中沒有出現,我有一個經驗法則來檢查命名空間。 – ChallengeAccepted
我知道它已經很長,但這個答案對我來說,現在的問題是我如何得到開關,以便我可以附加一個監聽程序編程? –
嘿「自定義佈局的菜單應該始終是RelativeLayout」。是否有任何文檔狀態? – Yeung
嗯,這是真的。如果我更改爲FrameLayout,則開關不易觸摸。 – Yeung
如果我使用LinearLayout,在這裏一切正常。謝謝你的回答! – Erwin