简体   繁体   English

上下文操作栏强制溢出

[英]Contextual action bar forces overflow

I am building the menu items of my Actionbar programmatically, due to a bug in Android when having two Fragments each with their own menu. 我正在以编程方式构建我的Actionbar的菜单项,因为Android中有两个Fragments每个Fragments都有自己的菜单。

I am building the menu in onCreateActionMode of the MultiChoiceModeListener . 我正在MultiChoiceModeListener onCreateActionMode中构建菜单。 If there are more than 4 menu items all of the items are forced into the overflow menu of the contextual action bar. 如果有超过4个菜单项,则所有项目都被强制进入上下文操作栏的溢出菜单。

This happens in portait and landscape mode, but only when the layout is in the non-tablet view (eg, both fragments are not displayed). 这在portait和横向模式中发生,但仅在布局处于非平板电脑视图中时(例如,两个片段均未显示)。 I'm testing on a Galaxy Nexus with Android 4.2 我正在使用Android 4.2测试Galaxy Nexus

    mListView = getListView();
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, android.view.Menu menu) {
            // Inflate the menu for the CAB
            menu.add(1, 0, 0, "Menu Item 1");
            menu.add(1, 1, 1, "Menu Item 2");
            menu.add(1, 2, 2, "Menu Item 3");
            menu.add(1, 3, 3, "Menu Item 4");
            menu.add(1, 4, 4, "Menu Item 5");
            return true;
        }
    }

UPDATE UPDATE

I've narrowed down this issue to if there are more than 4 menu items all items are force into overflow. 我已经缩小了这个问题,如果有超过4个菜单项,所有项目都会强行溢出。 The length of the text label doesn't matter. 文本标签的长度无关紧要。 I even set all the text to empty strings and they were still forced into overflow. 我甚至将所有文本设置为空字符串,它们仍然被强制溢出。 Setting IF_ROOM or ALWAYS doesn't seem to have an effect. 设置IF_ROOMALWAYS似乎没有效果。

UPDATE 2 更新2

I have another Fragment that is using a OnItemLongClickListener , instead of a MultiChoiceModeListener , and and I'm not experiencing the same issue. 我还有一个Fragment是使用OnItemLongClickListener代替, MultiChoiceModeListener ,和我不会遇到同样的问题。 I can put in 10 menu items and it will display the first couple in the Actionbar and put the rest into overflow, as expected. 我可以输入10个菜单项,它会在Actionbar显示第一对,并将其余部分按预期方式溢出。

UPDATE 3 更新3

A little more info on this issue, with a Nexus 7 in portrait mode the issue still exists, however, in the non-tablet view (both fragments are not displayed), in landscape mode on the Nexus 7, the menu items display. 关于这个问题的更多信息,在纵向模式下Nexus 7问题仍然存在,但是,在非平板电脑视图中(两个片段都不显示),在Nexus 7的横向模式下,菜单项显示。 The only thing I can think of is there is some spacing calculation going on when using a MultiChoiceModeListener that thinks there is no room in the contextual actionbar when there is. 我唯一能想到的是当使用MultiChoiceModeListener认为在上下文操作栏中没有空间时,会进行一些间距计算。

The Menu.add() methods all return MenuItem s. Menu.add()方法都返回MenuItem For each MenuItem added, just call MenuItem.setShowAsAction(int) like this: 对于添加的每个MenuItem,只需调用MenuItem.setShowAsAction(int),如下所示:

MenuItem menuItem = menu.add(1, 0, 0, "Menu Item 1");
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM