2012-01-31 35 views
11

如果Android ActionBar被拆分爲頂部和底部,使用Manifext.xml中的android:uiOptions="splitActionBarWhenNarrow", 有強制某些動作顯示在而不是將它們全部放在底部?如何將動作添加到拆分ActionBar的頂部

+0

貌似答案是「否」,並在 http://stackoverflow.com/questions/8571754/android-split-action-bar-with-action-items-items得到的回答頂部和底部 – 2012-01-31 09:49:13

回答

1

有沒有這樣做的標準方法。也就是說,操作欄自定義視圖將顯示在上方的欄中,因此您可以使用它。你會失去一些額外的東西(長按敬酒),所以你必須自己實施它們。也就是說,你使用的是ActionBarSherlock,所有的普通按鈕的佈局和樣式都在那裏,所以你可以使用它們。

2

是的!您可以在Manifest.xml中繼續使用android:uiOptions="splitActionBarWhenNarrow"

你只還需要設置:

// set the actionbar to use the custom view 
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

//set the custom view to use 
getActionBar().setCustomView(R.layout.custom_action_bar_top); 

R.layout.custom_action_bar_top將是擁有所有你希望出現在頂部操作欄中的按鈕的視圖。

在底部的所有菜單項都應該照常添加到您的活動的onCreateOptionsMenu方法中。

+1

嗨@immersion,這是有道理的,但你試過嗎?它實際上是這樣工作嗎? – ericn 2014-02-19 02:18:51

+2

嗨@fuzzybee,當我需要這樣做時,我只需要一個搜索欄,因此我使用了: 'SearchView searchView = new SearchView(context); getSupportActionBar()。setCustomView(searchView,\t new LayoutParams(Gravity.RIGHT));' 如果您想查看它的外觀,請查看[SwishPix](https://play.google.com/store/)應用程序/詳細信息?ID = com.immersion.swishpix.free)。專輯列表的屏幕截圖就是它可能看起來像的例子。 – 2014-02-19 02:41:44

+0

有了這個解決方案,您將不得不手動重新實現圖標和標題,因此它不會是一個簡單的插入式解決方案。 – Andres 2014-04-05 00:34:27

1

我想第二次迪倫華森的解決方案,但有一個改進。 對於那些想要保留標題,而不是用新視圖替換整個actionBar的用戶,他們使用getActionBar().setDisplayShowCustomEnabled(true)而不是getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);,因爲後者將導致新視圖成爲操作欄中顯示的唯一視圖。我的代碼:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = new SearchView(this); 
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    getActionBar().setDisplayShowCustomEnabled(true); 
    getActionBar().setCustomView(searchView, new ActionBar.LayoutParams(Gravity.RIGHT)); 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
+0

已確認,這將啓用自定義視圖,而不會禁用主屏幕按鈕和標題,如Dylan的答案中所示。如果標題仍未顯示,則可能會被自定義視圖覆蓋;這個答案提供了一些佈局選項:http://stackoverflow.com/a/16517395/462162 – arlomedia 2014-08-31 18:31:11