是否可以在布局中使用特定布局获取 Activity 的名称?
Is that possible to get the name of Activity using a specific layout, within the layout?
我正在开发一个应用程序,要求您登录并根据您的帐户启动 activity。问题是我有一个抽屉布局,我需要根据 activity 更改 menu.xml 文件。我知道可以在 XML 文件中使用条件结构。有没有办法在 XML 文件中检索调用 Activity 的名称?
这将允许我根据调用 XML 文件的 activity 的名称加载不同的菜单。
这是调用 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
我正在开发一个应用程序,要求您登录并根据您的帐户启动 activity。问题是我有一个抽屉布局,我需要根据 activity 更改 menu.xml 文件。我知道可以在 XML 文件中使用条件结构。有没有办法在 XML 文件中检索调用 Activity 的名称? 这将允许我根据调用 XML 文件的 activity 的名称加载不同的菜单。
这是调用 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