繁体   English   中英

如何关闭底部应用栏的导航抽屉

[英]How to close navigation drawer of bottom app bar

我在我的应用程序中使用Bottom App Bar作为导航组件。 单击导航抽屉的一项(从底部显示)时,应该会关闭导航抽屉,但事实并非如此。 单击“导航抽屉”的导航项目时,将打开相关的片段,但(底部应用程序栏的)“导航抽屉”仍会出现。

我尝试使用DrawerLayout和抽屉布局.closeDrawer(Gravity.LEFT),但是它不起作用。

这是布局:

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout
         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/drawerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

           <androidx.drawerlayout.widget.DrawerLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/drawerLayout"
              tools:openDrawer="left">

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


                  <com.google.android.material.navigation.NavigationView
                         android:id="@+id/navigation_view"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         app:headerLayout="@layout/nav_header_layout"
                         app:menu="@menu/bottom_nav_drawer_menu" >

        </com.google.android.material.navigation.NavigationView>
        </RelativeLayout>
   </androidx.drawerlayout.widget.DrawerLayout>
</RelativeLayout>

这是IDE给出错误的代码部分:

        navigation_view.setNavigationItemSelectedListener {

        drawerLayout.closeDrawer(Gravity.LEFT)

        ...

        return@setNavigationItemSelectedListener true

    }

出现以下错误:

java.lang.IllegalArgumentException:找不到重力为LEFT的抽屉视图

我在下面的链接上尝试过解决方案,但是它们没有用:

Android DrawerLayout-找不到带有重力的抽屉视图

您需要指定重力:

<com.google.android.material.navigation.NavigationView
      android:id="@+id/navigation_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="left"
      app:headerLayout="@layout/nav_header_layout"
      app:menu="@menu/bottom_nav_drawer_menu" >

另外,请考虑使用:

drawerLayout.closeDrawer(GravityCompat.LEFT)

我已经通过使用dismiss()函数而不是closeDrawer()解决了我的问题:

    navigation_view.setNavigationItemSelectedListener {

    ... 

    dismiss()

    ...

    return@setNavigationItemSelectedListener true

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM