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

时间:2017-01-11 11:13:30

标签: android android-toolbar android-optionsmenu

我想在一个OptionsMenu中使用两个Toolbar

来自Activity的第一个选项菜单,此菜单中的项目必须位于Toolbar的末尾。

来自不同Fragment's的第二个选项菜单(每个都有自己的菜单),必须在第一个菜单之前。

这看起来像: enter image description here

1 个答案:

答案 0 :(得分:1)

Activity

@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:showAsAction,此项目何时以及如何在应用栏中显示为操作项。仅当活动包含应用栏时,菜单项才会显示为操作项。有效值

  • always:始终将此项目放在应用栏中。除非项目始终出现在操作栏中,否则请避免使用此项。
  • collapseActionView:与此操作项关联的操作视图(由android:actionLayout或android:actionViewClass声明)是可折叠的。
  • ifRoom:只有在有空间的情况下才能将此项目放在应用栏中。
  • never:切勿将此项目放在应用栏中。而是在应用栏的溢出菜单中列出该项目。
  • withText:还包括带有操作项的标题文本(由android:title定义)。
相关问题