Android:根据所选片段更改菜单 [英] Android: change menu depending on selected fragment

查看:51
本文介绍了Android:根据所选片段更改菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的片段,但是我想更改选项菜单.我希望只有"Solicitudes"拥有它

I have a diferent fragments but I want to change the options menu. I want that only the "Solicitudes" have it

我要使用此选项的片段

此片段不应具有

我有一个包含main.xml的菜单文件夹,实际上我创建了另一个没有此设置选项的main2.xml,但我不知道如何更改

I have a menu folder with the main.xml and actually I create another main2.xml that doesn't have this settings option but I don't know how to change this

到目前为止,这是我的代码:

Here's my code so far:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mTitle = mDrawerTitle = getTitle();

    fragment = new History();

oncreate:

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

以及历史记录"片段的代码:

And the code for the History fragment:

public class History extends Fragment {
public static final String ARG_HISTORY = "arg_history";

public History() {
    // Empty constructor required for fragment subclasses
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.history, container, false);
    return rootView;
}

推荐答案

在您的 Fragment 中,您需要说出此 Fragment 控制菜单.

In your Fragment you need to say that this Fragment controls the menu.

在您的 Fragment onCreate 中.

setHasOptionsMenu(true);

现在,您可以在 Fragment 中实现以下内容,以隐藏不需要的 MenuItem .

Now you can implement the following in your Fragment to hide the MenuItem you don't want.

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.findItem(R.id.unwanted_item).setVisible(false);
    }

请确保您要在 MenuItem 中插入 Fragment 中的相反内容.

Make sure you do the reverse in the Fragment you do want the MenuItem in.

如果要添加不在 Activity 加载的 Menu 中的 MenuItem ,请在 onCreateOptionsMenu中执行以下操作在您的 Fragment 中.

If you want to add a MenuItem that is not in the Menu loaded by your Activity do the following in the onCreateOptionsMenu in your Fragment.

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

这篇关于Android:根据所选片段更改菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆