2012-10-06 66 views
2

我一直在開發Android應用程序,但我對Android的4個以上版本瞭解不多。因此,請大家幫我 - 我與標籤Android應用程序導航:如何將下拉菜單添加到Android項目的ActionBar中?

final ActionBar actionBar = getActionBar(); 
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

但我還需要下拉菜單添加到動作條的其他目標。我可以做嗎?請,如果可能的話,給我一個例子。先謝謝你。

回答

15

您可以使用一種稱爲android微調器的東西。它看起來像這樣:

enter image description here

您可以自定義它以滿足您的應用程序設計太多的方法。

這裏是一個偉大的教程由谷歌如何使用這些:如果你想要將它添加到動作欄中

http://developer.android.com/guide/topics/ui/controls/spinner.html

,您可以通過微調適配器做如下詳細說明: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

如果你想添加圖標做某些動作,然後就可以看到這一點: http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

如果你想d o在酒吧本身(如在谷歌搜索應用程序搜索)某些行爲,然後看到: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

如果你想添加導航選項卡,然後看到: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

+0

我知道這個元素的存在,但我不知道如何將微調添加到操作欄 – malcoauri

+0

它不符合我的任務。請告訴我,如何將TextView或圖標添加到ActionBar? – malcoauri

+0

增加了一些資源來幫助你。您不能將常規textView和圖標添加到操作欄,因爲它是一個特殊的UI元素,而不是普通的佈局。但是你可以添加某些東西來幫助你完成任務。 –

3

絕對是最好的,和我發現迄今爲止最簡單的答案就是在這裏。

基本上,在這種情況下不需要自定義佈局。只需設置actonViewClass:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:yourapp="http://schemas.android.com/apk/res-auto" > 

    <item android:id="@+id/spinner" 
    yourapp:showAsAction="ifRoom" 
    yourapp:actionViewClass="android.widget.Spinner" /> 
</menu> 

再處理它在onCreateOptionsMenu,像往常一樣:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_layout, menu); 
    MenuItem item = menu.findItem(R.id.spinner); 
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item); 
    spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content 
    s.setOnItemSelectedListener(onItemSelectedListener); // set the listener, to perform actions based on item selection 

這是迄今爲止最簡單和乾淨的解決方案。對原作者FrançoisPOYER的致謝。

+1

爲什麼不直接鏈接到答案呢? – Mathemats

+0

如果我會給出答案,你會說鏈接不可靠:) –

+0

那麼爲什麼不鏈接它的評論? – Onik

相關問題