是否可以在布局内使用特定布局获取活动名称



我正在开发一个应用程序,要求您登录,并根据您的帐户启动活动。问题是我有一个抽屉布局,我需要根据活动更改菜单.xml文件。我知道可以在XML文件中使用条件结构。有没有办法在XML文件中检索调用活动的名称?这将允许我根据调用 XML 文件的活动的名称加载不同的菜单。

这是调用menu_main.xml文件的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
    style="@style/nav_view"
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:itemTextColor="#FFFFFF"
    app:menu="@menu/menu_main"/>

和XML文件:

<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group
    android:checkableBehavior="single"
    >
    <item
        android:id="@+id/nav_profile"
        android:checked="true"
        android:title="@string/nav_profile"/>
    <item
        android:id="@+id/nav_search"
        android:title="@string/nav_organisation"/>
    <item
        android:id="@+id/nav_logout"
        android:title="@string/nav_logout"
        />
</group>

提前感谢!

你真的想在 xml 中做这样的事情吗?您可以动态设置菜单 prom java 代码:

navigationView.getMenu().clear(); //clear old items
navigationView.inflateMenu(R.menu.new_navigation_drawer_items); //inflate new menu

Menu menu = navigationView.getMenu(); //get current menu
menu.add();//add an item

最新更新