简体   繁体   English

如何在片段中使用与活动不同的菜单?

[英]How to use different menu in fragment than in activity?

I want to have a menu for my main activity and a menu for a fragment which is contained in the main activity.我想要一个用于我的主要活动的菜单和一个用于包含在主要活动中的片段的菜单。 The main activity menu should be removed and the fragment menu inflated when the fragment is visible.应删除主活动菜单,并在片段可见时扩充片段菜单。 I have tried to achieve this however this is the result:我试图实现这一点,但这是结果:

Main activity menu snippet by itself:主要活动菜单片段本身:

在此处输入图像描述

Main activity menu snippet when fragment is displayed:显示片段时的主要活动菜单片段:

在此处输入图像描述

Clearly instead of replacing the menu in the main activity I am adding to it despite having created and inflated a fragment menu.显然,尽管创建并扩充了片段菜单,但我并没有替换主要活动中的菜单,而是将其添加到其中。

Here is the code in the activity responsible for this:这是负责此活动的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // Depending on which fragment is used display different actions.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Here is the code in the fragment responsible for this:这是片段中负责此操作的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.friend_menu, menu);
}

Here is friend_menu.xml:这是 friend_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
    android:icon="@drawable/ic_person_add_black_24dp"
    android:id="@+id/nav_person_add"
    android:title="Add people"
    app:showAsAction="ifRoom" />

</menu>

How do I get my desired menu (without the search action and with the add_friend action)?我如何获得我想要的菜单(没有搜索操作和 add_friend 操作)? I realise that it may be simpler to manage this from the activity and only inflate specific fragment menus when the fragment is displayed like the comment in the activity says: // Depending on which fragment is used display different actions.我意识到从活动中管理它可能更简单,并且只在显示片段时才膨胀特定的片段菜单,就像活动中的评论说的那样: // Depending on which fragment is used display different actions.

You could try to clear() the menu if you want to keep your structure that way. 如果你想保持你的结构,你可以尝试clear()菜单。

In your Fragment 在你的Fragment

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear(); // Remove all existing items from the menu, leaving it empty as if it had just been created.
    inflater.inflate(R.menu.friend_menu, menu);
}

What worked for me什么对我有用

in your onCreateView在你的onCreateView

 public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

     setHasOptionsMenu(true);
 }

in your onCreateOptionsMenu在你的onCreateOptionsMenu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.setGroupVisible(0,false);
    inflater.inflate(R.menu.new_menu, menu);

    super.onCreateOptionsMenu(menu, inflater);
}

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

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