Android的操作栏溢出菜单在纵向模式下不显示

Android的操作栏溢出菜单在纵向模式下不显示

本文介绍了Android的操作栏溢出菜单在纵向模式下不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的Andr​​oid操作栏(我不使用兼容性库)与搜索查看,并极少数的纯文本菜单项。我想始终扩大了搜索查看,并在溢出菜单中的剩余项目。在code我至今工作正常,在平板电脑上,并在我的Nexus 5在横向模式,但是当手机处于纵向模式时,不显示溢出菜单。如果我在 collapseActionView 属性添加到搜索查看,正确显示溢出菜单,但我想保持搜索窗口小部件完全展开。

这里的XML我的选项菜单:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID / menu_search
          机器人:图标=@机器人:可绘制/ ic_menu_search
          机器人:标题=@字符串/搜索
          机器人:showAsAction =ifRoom
          机器人:actionViewClass =android.widget.SearchView/>
    <项目机器人:ID =@ + ID / action_clear_history
          机器人:标题=@字符串/ action_clear_history
          机器人:showAsAction =从不/>
    <项目机器人:ID =@ + ID / action_feedback
          机器人:标题=@字符串/ action_feedback
          机器人:showAsAction =从不/>
    <项目机器人:ID =@ + ID / action_about
          机器人:标题=@字符串/ action_about
          机器人:showAsAction =从不/>
< /菜单>
 

这是我的 onCreateOptionsMenu code:

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    如果(!mNavigationDrawerFragment.isDrawerOpen()){
        。getMenuInflater()膨胀(R.menu.options_menu,菜单);
        SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
        搜索查看搜索查看=(搜索查看)menu.findItem(R.id.menu_search).getActionView();
        sea​​rchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        sea​​rchView.setIconifiedByDefault(假);
        restoreActionBar();
        返回true;
    }
    返回super.onCreateOptionsMenu(菜单);
}
 

这里是风景模式截图,按预期工作:

和这里的肖像模式,具有溢出菜单丢失:

任何人都可以点我朝着正确的方向?

解决方案

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android >
    <项目机器人:ID =@ + ID / menu_search
          机器人:图标=@机器人:可绘制/ ic_menu_search
          机器人:标题=@字符串/搜索
          机器人:showAsAction =ifRoom
          机器人:actionViewClass =android.widget.SearchView/>
    <项目
          机器人:ID =@ + ID / menu_item_more
          机器人:图标=@可绘制/ ic_overflow
          机器人:showAsAction =总是
          机器人:标题=点击查看更多选项>
        <菜单>
            <项目机器人:ID =@ + ID / action_clear_history
                  机器人:标题=@字符串/ action_clear_history
                  机器人:showAsAction =从不/>
            <项目机器人:ID =@ + ID / action_feedback
                  机器人:标题=@字符串/ action_feedback
                  机器人:showAsAction =从不/>
            <项目机器人:ID =@ + ID / action_about
                  机器人:标题=@字符串/ action_about
                  机器人:showAsAction =从不/>
        < /菜单>
    < /项目>
< /菜单>
 

我添加了一个项目持有引用溢出子菜单中,你可以试试这个,如果想要的,或者可以问疑问的。和 ic_overflow 图标可能是

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?

解决方案
<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

这篇关于Android的操作栏溢出菜单在纵向模式下不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:36