如何在Android中设置带有导航抽屉的导航组件?
如何使用导航抽屉设置导航组件? 如何在我的应用程序中使用它 每件事都能通过一项活动完成吗 如何处理只有一个活动和具有动态工具栏可见性的片段的工具栏可见性。此外,还有一些碎片,我需要关闭抽屉,使其无法接近 这个问题是一个自我回答的问题,更像是一个教程,而不是现实生活中的QA 如何使用导航抽屉设置导航组件 如何在我的应用程序中使用它 导航抽屉的设置在导航组件方面略有不同 注意,如果您使用抽屉导航创建新应用程序,则不需要当前教程。然而,我将解释一些可能看起来很奇怪的事情,如果你决定在应用程序的后期添加抽屉 首先,您需要设置如何在Android中设置带有导航抽屉的导航组件?,android,navigation-drawer,android-architecture-navigation,Android,Navigation Drawer,Android Architecture Navigation,如何使用导航抽屉设置导航组件? 如何在我的应用程序中使用它 每件事都能通过一项活动完成吗 如何处理只有一个活动和具有动态工具栏可见性的片段的工具栏可见性。此外,还有一些碎片,我需要关闭抽屉,使其无法接近 这个问题是一个自我回答的问题,更像是一个教程,而不是现实生活中的QA 如何使用导航抽屉设置导航组件 如何在我的应用程序中使用它 导航抽屉的设置在导航组件方面略有不同 注意,如果您使用抽屉导航创建新应用程序,则不需要当前教程。然而,我将解释一些可能看起来很奇怪的事情,如果你决定在应用程序的后期添加
activity\u main.xml
和MainActivity
以便为导航架构做好准备:
<?xml version="1.0" encoding="utf-8"?>
<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">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
而content\u main
是存放碎片的地方:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果您注意到NavigationView
标记中的app:menu=“@menu/activity\u main\u drawer”
,则片段名称应与mobile\u navigation.xml中的片段名称相同:
android:theme="@style/AppTheme.NoActionBar"
<group android:checkableBehavior="single">
<item
android:id="@+id/homeFragment"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/galleryFragment"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/menu_gallery" />
<item
android:id="@+id/slideshowFragment"
android:icon="@drawable/ic_menu_slideshow"
android:title="@string/menu_slideshow" />
<item
android:id="@+id/toolsFragment"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/shareFragment"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
<item
android:id="@+id/sendFragment"
android:icon="@drawable/ic_menu_send"
android:title="@string/menu_send" />
</menu>
</item>
</menu>
然后在onCreate
方法中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar) //set the toolbar
drawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment,
R.id.galleryFragment,
R.id.slideShowFragment,
R.id.toolsFragment,
R.id.shareFragment,
R.id.sendFragment,
R.id.loginFragment,
R.id.phoneConfirmationFragment
), drawerLayout
)
setupActionBarWithNavController(navController!!, appBarConfiguration!!) //the most important part
navView.setupWithNavController(navController!!) //the second most important part
//other things unrelated
}
让我们看看这里发生了什么:
首先,您需要一个对navController
的引用。AppBarConfiguration
只是一个类,它保存将作为顶级目的地打开的片段。这意味着在它们之后打开的片段将从片段后堆栈中释放当前片段。告诉AppBarConfiguration
我们还有一个抽屉(作为构造函数中的参数传递),这一点很重要
下面是一个名为“onSupportNavigateUp()”
”的方法:
此方法与“后退”按钮有关。但是如果你有抽屉导航,你就不会太需要它了。当您在backbackback(或至少两个)上添加了很多片段时,这真的很方便
每件事都能通过一项活动完成吗
是的,当然!但是当涉及到条件导航时,它仍然需要做更多的工作。比如,当你想显示不属于抽屉应用程序的片段时。但谷歌仍然在这方面取得了巨大的进步。您可以参考条件导航
如何仅通过一个活动和
具有动态工具栏可见性的片段。还有,
我需要这些碎片来关闭抽屉,让它无法进入
您可以从navController
使用addOnDestinationChangedListener
:
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.loginFragment, R.id.registerFragment, R.id.phoneConfirmationFragment -> {
toolbar?.visibility = View.GONE
drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
else -> {
toolbar?.visibility = View.VISIBLE
drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}
}
}
现在你的应用程序上有了一个抽屉和导航组件。您好,我遵循了与您相同的所有步骤,我甚至看到了汉堡包图标,但单击它,导航抽屉不会打开。有什么我可能遗漏的吗?单击时检查。它可能与IsOpen
/isClosed
属性有关。在OnOptions ItemSelected
中捕获单击,因为我返回的是true
,而不是super.OnOptions ItemSelected(item)
。因此,单击时没有发生任何事情。无论如何,谢谢你,你的回答帮了大忙。setupActionBarWithNavController(navController!!,appBarConfiguration!!)
此方法不再有效,需要像setupActionBarWithNavController(this,navController!!,appBarConfiguration!!)这样传递上下文。
I我的应用程序。我想在单击2导航抽屉菜单时显示一个祝酒词,另一个想打开一个片段。我该怎么办?
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar) //set the toolbar
drawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment,
R.id.galleryFragment,
R.id.slideShowFragment,
R.id.toolsFragment,
R.id.shareFragment,
R.id.sendFragment,
R.id.loginFragment,
R.id.phoneConfirmationFragment
), drawerLayout
)
setupActionBarWithNavController(navController!!, appBarConfiguration!!) //the most important part
navView.setupWithNavController(navController!!) //the second most important part
//other things unrelated
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration!!) || super.onSupportNavigateUp()
}
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.loginFragment, R.id.registerFragment, R.id.phoneConfirmationFragment -> {
toolbar?.visibility = View.GONE
drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
else -> {
toolbar?.visibility = View.VISIBLE
drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}
}
}