【问题标题】:How to use Bottom Navigation with Navigation Drawer如何使用带有导航抽屉的底部导航
【发布时间】:2019-05-30 20:28:49
【问题描述】:

我正在尝试添加带有底部导航视图的导航抽屉。我对所有底部导航视图片段都有全屏活动。我在左上方有应用程序图标。当用户单击该图标时,我想打开导航抽屉。那么我该如何实现呢?

我尝试了 DrawerLayout。但它包括工具栏。我试图在点击导航抽屉的项目时打开新的活动,但它给出了 IllegleStateException。

【问题讨论】:

标签: java android xml layout


【解决方案1】:

我在下面分享了以下代码。您必须将 Nav Drawer 的汉堡菜单图标放在工具栏中,并在 DrawerLayout 内放置一个 NavigationView,其中有一个要以编程方式填充的子项(ListView、RecyclerView、ExpandableListView 等),或者您可以通过设置在 NavigationView 中设置静态项NavigationView 中的菜单。希望有帮助。如果您还有其他疑问,请询问。

<androidx.drawerlayout.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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorActivityBackground"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:layoutDirection="locale"
        android:orientation="vertical"
        android:textDirection="locale">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/topbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                app:contentInsetEnd="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetRight="0dp"
                app:contentInsetStart="0dp"/>
        </RelativeLayout>
    </FrameLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:maxWidth="320dp">

        <ExpandableListView
            android:id="@+id/navList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:background="@color/white"
            android:groupIndicator="@null" />

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

【讨论】:

  • 感谢您的回复。但是在 main_activity 中的底部导航视图,我正在主框架布局中加载片段。并且所有活动都是全屏活动。我只想单击 main_activity 中的图像并打开导航抽屉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多