简体   繁体   English

限制Android中可见上下文菜单项的数量

[英]Limit number of visible context menu items in Android

The number of option menu items can change on runtime in my app, depending on the actions of the user. 选项菜单项的数量可以在运行时在我的应用程序中更改,具体取决于用户的操作。

I am using this right now for my menu items : 我现在正在使用此菜单项:

app:showAsAction="ifRoom"

If there is enough room in some cases there are 4 menu items visible in the actionbar. 如果在某些情况下有足够的空间,则在操作栏中可以看到4个菜单项。 Is there a way to limit the visible menu items? 有没有办法限制可见菜单项?

In my case, I want maximum 2 menu items visible in the actionbar, the additional entries should be in the "more" section. 就我而言,我希望动作栏中最多显示2个菜单项,其他条目应在“更多”部分中。

You can force maximum two visible MenuItem as action buttons on the actionbar by setting: 您可以通过设置以下操作来强制最多两个可见的MenuItem作为操作栏上的操作按钮:

app:showAsAction="never"

for all the menu items, except the first two. 所有菜单项,前两个除外。

As mentioned in the Android docs : 如Android 文档中所述

never : Never place this item in the app bar. never :永远不要将此项目放在应用程序栏中。 Instead, list the item in the app bar's overflow menu. 而是在应用程序栏的溢出菜单中列出该项目。

Currently you're using ifRoom keyword, which will show the menu item in the action bar if there's enough space available for it. 当前,您正在使用ifRoom关键字,如果有足够的可用空间,它将在操作栏中显示菜单项。 So, you won't be able to force max two items using this. 因此,您将无法使用此强制最多两个项目。 From docs : 文档

ifRoom : Only place this item in the app bar if there is room for it. ifRoom :仅在有ifRoom空间放置此项目时,才将其放置在应用程序栏中。 If there is not room for all the items marked "ifRoom", the items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu. 如果没有所有标记为“ ifRoom”的项目的空间,则将具有最低orderInCategory值的项目显示为操作,其余项目显示在溢出菜单中。

PS : What you're calling more section is referred as overflow menu in the above text and in Android. PS:您所说的“ 更多”部分在上述文本和Android中称为“ 溢出”菜单

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

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