我有一个基于应用程序中的碎片,其中一些碎片有我想提出的按钮进入其他操作的动作条
,而一些不具备任何此类行动。我已经创建了一个包含所有项目的所有片段的XML菜单文件和I显示/隐藏在运行时根据所使用的片段的组合是当前显示:
I have a fragments based app where some fragments have additional actions that I want to put as buttons into the ActionBar
, while some do not have any such actions. I have created an xml menu file that contains all the items for all the fragments and I show/hide at runtime depending on which fragment is currently shown using a combination of:
MenuItem item = menu.findItem(some_id);
item.setVisible(false);
MenuItem item = menu.findItem(some_other_id);
item.setVisible(true);
现在的问题是,当我看,有没有菜单项,如果我改变设备的方向,然后更改为不同片段的片段 - 与菜单项目 - 这些项目不会在出现在动作条
,但只能访问使用该设备的菜单按钮。
The problem is that when I am viewing a fragment that has no menu items, if I change device orientation and then change to a different fragment - with menu items - those items do not show up in the ActionBar
but are only accessible using the device's "Menu" button.
所有的菜单项都设置为安卓showAsAction =总是
在XML文件中,但他们始终没有出现在动作条
如果在观看片段,没有菜单项的最后一个方向的改变已完成。这发生在两个人像到风景和景观到人像的变化。
All menu items are set as android:showAsAction="always"
in the xml file, yet they consistently fail to show up in the ActionBar
if the last orientation change was done while viewing a fragment with no menu items. This happens during both portrait-to-landscape and landscape-to-portrait changes.
我如何可以强制在任何时候都显示在动作条
的项目?
How can I force the items to be displayed in the ActionBar
at all times?
两点:
按照动作条设计文件,景观甚至是10平板电脑将只显示最多4个项目,每下面的图表中的操作栏: 假设你正在项目,显示了最大数量的动作条
,如果你想片段项添加到动作条按照片段 - 添加项目到操作栏指南
Per the ActionBar design document, even a 10" tablet in landscape will only show a maximum of 4 items in the Action Bar per the below chart:
Assuming you are under the maximum number of items that show in the ActionBar
, if you want Fragments to add items to the ActionBar
when they appear and remove when they hidden, you do not need to manually show/hide items within a single xml menu file, but instead add the items in each individual Fragment
per the Fragments - Adding items to the Action Bar guide.