[英]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的抽屜視圖
我在下面的鏈接上嘗試過解決方案,但是它們沒有用:
您需要指定重力:
<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.