如何在NavigationView中获取菜单项? [英] How can I get menu item in NavigationView?

本文介绍了如何在NavigationView中获取菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<android.support.design.widget.NavigationView
    android:id="@+id/drawer_nav"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/menu_drawer"
    />

我正在使用android.support.design.library进行材料设计.

I am using android.support.design.library for material design.

我想要的是在用户未登录时隐藏一些菜单项.

What I want is to hide some menu items when the user is not logged-in.

现在我很难在NavigationView中获得菜单项.

Now I have trouble to get the menu item in NavigationView.

我尝试过:

MenuItem logoutItem = (MenuItem) mNavigationView.findViewById(R.id.menu_logout);
logoutItem.setVisible(false);

但是它不起作用.

我该怎么做?

谢谢.

推荐答案

您可以通过

You can get that by method of NavigationView.getMenu()

Menu menuNav = mNavigationView.getMenu();

然后您可以通过以下方式找到特定的项目

Then you can find specific item by

MenuItem logoutItem = menuNav.findItem(R.id.menu_logout);

这篇关于如何在NavigationView中获取菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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