Android 如何在一个工具栏中使用两个选项菜单?

Android 如何在一个工具栏中使用两个选项菜单?,android,android-toolbar,android-optionsmenu,Android,Android Toolbar,Android Optionsmenu,我想在一个工具栏中使用两个选项menu 第一个选项是活动中的菜单,此菜单中的项目必须位于工具栏的末尾 来自不同片段的第二个选项菜单(每个都有自己的菜单),并且必须位于第一个菜单之前 这看起来像: 在活动中: @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.

我想在一个
工具栏中使用两个
选项menu

第一个选项是
活动
中的菜单,此菜单中的项目必须位于
工具栏
的末尾

来自不同
片段的第二个选项菜单(每个都有自己的菜单),并且必须位于第一个菜单之前

这看起来像:
活动中:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
res/menu/menu.xml
中:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.nexttek.android.menu.activity.YourActivity">
    <item
        android:id="@+id/action_one"
        android:orderInCategory="100"
        android:title="@string/action_one"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_two"
        android:icon="@drawable/ic_image"
        android:orderInCategory="100"
        android:title="@string/action_two"
        app:showAsAction="always" />
</menu>

通过
app:showasaaction
,此项应在何时以及如何在应用程序栏中显示为操作项。只有当活动包含应用程序栏时,菜单项才能显示为操作项。有效值

  • 始终
    :始终将此项目放置在应用程序栏中。除非项目始终出现在操作栏中,否则请避免使用此选项
  • collapseActionView
    :与此操作项关联的操作视图(由android:actionLayout或android:actionViewClass声明)是可折叠的
  • ifRoom
    :仅当有空间放置此项目时,才将其放置在应用程序栏中
  • 从不
    :从不将此项目放在应用程序栏中。相反,在应用程序栏的溢出菜单中列出该项
  • withText
    :还包括标题文本(由android:title定义)和操作项