当前,我在底部导航中有三个选项,并为它们提供了导航图。
我的mainActivity.xml文件的部分如下所示:
<fragment
android:id = "@+id/nav_host_fragment"
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "1"
android:name = "androidx.navigation.fragment.NavHostFragment"
app:navGraph = "@navigation/nav_graph"
app:defaultNavHost = "true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id = "@+id/bottom_nav"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
app:menu = "@menu/bottom_nav" />
在我的mainActivity中,我编写了这段代码,
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
bottom_nav.setupWithNavController(navController)
NavigationUI.setupActionBarWithNavController(this, navController)
与导航有关的所有事情都由jetpack导航库处理。 现在我还想添加一个导航抽屉,并且在抽屉中我想添加不同的菜单项(不仅是底部导航中的三个菜单项),所以我将为导航抽屉添加新的菜单资源文件,现在我应该如何使用导航库底部导航和导航抽屉?我不想手动执行片段事务并使用片段管理器。
我能想到的一种方法是将所有片段添加到单个导航图中(当前用于底部导航),然后将相同的navController用于导航抽屉,但是我正在寻找一种更好的方法。 / p>
答案 0 :(得分:0)
抽屉的结构是您必须在抽屉布局中包括活动。因此,在您的情况下,底部导航的实现是您包括bottom_navigation INSIDE main_activity ---> main_activity INSIDE抽屉布局
代码示例
<?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"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
SRC https://code.tutsplus.com/tutorials/how-to-code-a-navigation-drawer-in-an-android-app--cms-30263