如何删除导航抽屉?

Posted

技术标签:

【中文标题】如何删除导航抽屉?【英文标题】:How to remove navigation drawer? 【发布时间】:2021-05-12 02:56:48 【问题描述】:

我想从我的 fragment_loginfragment_create_account 片段中删除导航抽屉。我使用 addOnDestinationChangedListener 从这些片段中删除了底部导航和工具栏,工具栏代码如下:

navController.addOnDestinationChangedListener  _, destination, _ ->
            when(destination.id) 
                R.id.loginFragment, R.id.createAccountFragment ->
                    toolbar.visibility = View.GONE
                else -> toolbar.visibility = View.VISIBLE
            
        

同样,对于底部导航:

navController.addOnDestinationChangedListener  _, destination, _ ->
            when(destination.id) 
                R.id.loginFragment, R.id.createAccountFragment ->
                    bottomNavigationView.visibility = View.GONE
                else -> bottomNavigationView.visibility = View.VISIBLE
            
        

我尝试对导航抽屉执行相同的操作(与上述抽屉布局的代码相同),但没有成功,整个应用程序变为空白。任何帮助表示赞赏。

MainActivity.KT

package com.example.integratedmodulateoroperationroom

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.*
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() 
    private lateinit var navController: NavController
    private lateinit var appBarConfiguration: AppBarConfiguration
    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.findNavController()

        appBarConfiguration = AppBarConfiguration(
            setOf(R.id.homeFragment, R.id.loginFragment),
            drawer_layout
        )

        setSupportActionBar(toolbar)
        setupActionBarWithNavController(navController, appBarConfiguration)

        navController.addOnDestinationChangedListener  _, destination, _ ->
            when(destination.id) 
                R.id.loginFragment, R.id.createAccountFragment ->
                    toolbar.visibility = View.GONE
                else -> toolbar.visibility = View.VISIBLE
            
        

        bottom_nav.setupWithNavController(navController)
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)

        navController.addOnDestinationChangedListener  _, destination, _ ->
            when(destination.id) 
                R.id.loginFragment, R.id.createAccountFragment ->
                    bottomNavigationView.visibility = View.GONE
                else -> bottomNavigationView.visibility = View.VISIBLE
            
        

        nav_view.setupWithNavController(navController)
    

    override fun onCreateOptionsMenu(menu: Menu?): Boolean 
        menuInflater.inflate(R.menu.options_menu, menu)
        return true
    

    override fun onOptionsItemSelected(item: MenuItem): Boolean 
        return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
    

    override fun onSupportNavigateUp(): Boolean 
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    

activitymain.XML:

<?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_
    android:layout_
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:background="@drawable/toolbar_backgroud"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_
        android:layout_
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_gravity="start"
        app:menu="@menu/drawer_nav_menu"
        android:layout_
        android:layout_
        app:headerLayout="@layout/drawer_header"/>

</androidx.drawerlayout.widget.DrawerLayout>

nav_grah.XML

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.integratedmodulateoroperationroom.HomeFragment"
        android:label="Integrated Module OR (Operation Room)"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_homeFragment_to_otFragment"
            app:destination="@id/otFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right" />
        <action
            android:id="@+id/action_homeFragment_to_surgicalFragment"
            app:destination="@id/surgicalFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right" />
        <action
            android:id="@+id/action_homeFragment_to_laminarFragment"
            app:destination="@id/laminarFragment" />
    </fragment>
    <fragment
        android:id="@+id/otFragment"
        android:name="com.example.integratedmodulateoroperationroom.OtFragment"
        android:label="OT Lights"
        tools:layout="@layout/fragment_ot" />
    <fragment
        android:id="@+id/surgicalFragment"
        android:name="com.example.integratedmodulateoroperationroom.SurgicalFragment"
        android:label="Surgical Lights"
        tools:layout="@layout/fragment_surgical" />
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.integratedmodulateoroperationroom.SettingsFragment"
        android:label="Settings"
        tools:layout="@layout/fragment_settings" />
    <fragment
        android:id="@+id/laminarFragment"
        android:name="com.example.integratedmodulateoroperationroom.LaminarFragment"
        android:label="Laminar Airflow"
        tools:layout="@layout/fragment_laminar" />
    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.integratedmodulateoroperationroom.LoginFragment"
        android:label="Login"
        tools:layout="@layout/fragment_login" >
        <action
            android:id="@+id/action_loginFragment_to_homeFragment"
            app:destination="@id/homeFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/homeFragment"
            app:popUpToInclusive="true" />
        <action
            android:id="@+id/action_loginFragment_to_createAccountFragment"
            app:destination="@id/createAccountFragment" />
    </fragment>
    <fragment
        android:id="@+id/aboutFragment"
        android:name="com.example.integratedmodulateoroperationroom.AboutFragment"
        android:label="About"
        tools:layout="@layout/fragment_about" />
    <fragment
        android:id="@+id/createAccountFragment"
        android:name="com.example.integratedmodulateoroperationroom.CreateAccountFragment"
        android:label="CreateAccountFragment"
        tools:layout="@layout/fragment_create_account">
        <action
            android:id="@+id/action_createAccountFragment_to_loginFragment"
            app:destination="@id/loginFragment"
            app:popUpTo="@id/loginFragment"
            app:popUpToInclusive="true" />
    </fragment>
</navigation>

【问题讨论】:

你不要,把它锁起来。 【参考方案1】:

我不明白你要做什么,但你可以通过删除资源/菜单文件中的 menu.xml 来删除它。

不要忘记清理 XML 和类以避免任何错误。

如果您只是想从 UI 中隐藏它们,您可以通过设置可见性来做到这一点。

另一种方法是从主机屏幕中删除设置菜单。

【讨论】:

实际上我有多个片段,我试图从两个片段中删除它。我尝试设置可见性,但随后整个应用程序变为空白......如果你能展示如何设置可见性会很棒......我想我可能做错了什么 也许你应该创建一个回调来告诉主机屏幕隐藏底部导航,如果这个片段出现在用户面前。【参考方案2】:

我不知道我必须锁定导航抽屉。所以,通过使用

drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)

我成功隐藏了导航抽屉。所以,为了让它出现在其余的片段中,我使用了下面的代码:

navController.addOnDestinationChangedListener  _, destination, _ ->
            when(destination.id) 
                R.id.loginFragment, R.id.createAccountFragment ->
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                else -> drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            
        
    

【讨论】:

以上是关于如何删除导航抽屉?的主要内容,如果未能解决你的问题,请参考以下文章

无法删除 Vuetify 导航抽屉上的滚动条

单击 ImageView 时如何打开滑动导航抽屉?

删除导航抽屉中的“设置”选项卡

如何通过android上的复选框控制导航抽屉项目

Android 移除导航抽屉上的阴影

导航抽屉和工具栏出现在初始屏幕中