问题
I have a standard Android action bar (I'm not using the compatibility library) with a SearchView and a handful of text-only menu items. I'd like to have the SearchView always expanded and the remaining items in the overflow menu. The code I have so far works fine on a tablet and on my Nexus 5 in landscape mode, but when the phone is in portrait mode, the overflow menu isn't displayed. If I add the collapseActionView attribute to the SearchView, the overflow menu is properly displayed, but I'd like to keep the search widget fully expanded.
Here's the XML for my options menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item android:id="@+id/action_clear_history"
android:title="@string/action_clear_history"
android:showAsAction="never"/>
<item android:id="@+id/action_feedback"
android:title="@string/action_feedback"
android:showAsAction="never"/>
<item android:id="@+id/action_about"
android:title="@string/action_about"
android:showAsAction="never"/>
</menu>
And here's my onCreateOptionsMenu code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
Here's a screenshot of landscape mode, working as intended:
And here's portrait mode, with the overflow menu missing:
Can anyone point me in the right direction?
回答1:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"/>
<item
android:id="@+id/menu_item_more"
android:icon="@drawable/ic_overflow"
android:showAsAction="always"
android:title="CLICK FOR MORE OPTIONS">
<menu>
<item android:id="@+id/action_clear_history"
android:title="@string/action_clear_history"
android:showAsAction="never"/>
<item android:id="@+id/action_feedback"
android:title="@string/action_feedback"
android:showAsAction="never"/>
<item android:id="@+id/action_about"
android:title="@string/action_about"
android:showAsAction="never"/>
</menu>
</item>
</menu>
I've added an item which holds a reference to overflow sub menu you can try this, if wanted, or can ask doubt about it. and ic_overflow icon could be
回答2:
I had similar issue, usefull SO I have found was this : SearchView taking all the space in the new ActionBarCompat, so the solution is to add SearchView manually from code, and not from you menu layout.
here is some code I am using with success on api 19 in portrait, in fragment with support actionbar:
public void initSearchView() {
searchView = new SearchView(getActionBarHelper().getThemedContext());
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
searchView.setLayoutParams(layoutParams);
getActionBarHelper().setCustomView(searchView);
getActionBarHelper().setDisplayShowCustomEnabled(true);
searchView.setOnQueryTextListener(mOnQueryTextListener);
searchView.clearFocus();
searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
}
protected ActionBar getActionBarHelper() {
return ((ActivityBase) getActivity()).getSupportActionBar();
}
I call initSearchView in onCreateView(),
回答3:
try
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/20819289/android-action-bar-overflow-menu-not-displayed-in-portrait-mode