Android 如何从登录屏幕中删除底部导航栏

Android 如何从登录屏幕中删除底部导航栏,android,android-studio,kotlin,android-fragments,bottomnavigationview,Android,Android Studio,Kotlin,Android Fragments,Bottomnavigationview,嗨, 我在大学项目中面临一个问题。在上图中,您可以看到,在我的登录屏幕中有一个底部导航栏,我想删除它,但我尝试过的所有内容最终都出现了编译问题。 我的主要活动是 package com.example.integratedmodulateoroperationroom import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.Menu import android.v

嗨, 我在大学项目中面临一个问题。在上图中,您可以看到,在我的登录屏幕中有一个底部导航栏,我想删除它,但我尝试过的所有内容最终都出现了编译问题。 我的主要活动是

package com.example.integratedmodulateoroperationroom

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.*
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)

        bottom_nav.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()
    }
}
Main_Activity.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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/design_default_color_primary"
        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_width="match_parent"
        android:layout_height="0dp"
        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_width="match_parent"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>
破片登录

<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"
    tools:context=".LoginFragment">

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Login ID"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

    <Button
        android:id="@+id/button_goToHomeScreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Login"
        app:backgroundTint="#9E9E9E"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="274dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/logo"
        app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

导航图

<?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/homeFragment">

    <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" />
    </fragment>
</navigation>

NavController
提供了一个
OnDestinationChangedListener
接口,当NavController的当前目标或其参数更改时调用该接口,可用于在片段之间导航时更新UI元素,如
BottomNavigationBar

MainActivity.KT

// in oncreate

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

navController.addOnDestinationChangedListener { _, destination, _ ->
   bottomNavigationView.visibility = if(destination.id == R.id.loginFragment) {
       View.GONE
   } else {
       View.VISIBLE
   }
}
//在oncreate中
private val bottomNavigationView=findviewbyd(R.id.bottom_nav)
navController.addOnDestinationChangedListener{{,目标,{->
bottomNavigationView.visibility=if(destination.id==R.id.loginFragment){
视图消失了
}否则{
View.VISIBLE
}
}

链接到官方文档并查看更多信息,可以在活动的onCreate中执行类似操作。当选择导航栏中的任何项目时,它将根据片段id显示或隐藏导航

  private fun setupNav() {
    val navController = findNavController(R.id.nav_host_fragment)
    findViewById<BottomNavigationView>(R.id.bottomNav)
        .setupWithNavController(navController)

    navController.addOnDestinationChangedListener { _, destination, _ ->
        when (destination.id) {
            R.id.mainFragment -> showBottomNav()
            R.id.mineFragment -> showBottomNav()
            else -> hideBottomNav()
        }
    }
}

private fun showBottomNav() {
    bottomNav.visibility = View.VISIBLE

}

private fun hideBottomNav() {
    bottomNav.visibility = View.GONE

}
private fun setupNav(){
val navController=findNavController(R.id.nav_主机_片段)
findViewById(R.id.bottomNav)
.setupWithNavController(navController)
navController.addOnDestinationChangedListener{{,目标,{->
何时(destination.id){
R.id.mainFragment->showBottomNav()
R.id.mineFragment->showBottomNav()
else->hideBottomNav()
}
}
}
私人娱乐节目{
bottomNav.visibility=View.VISIBLE
}
私人娱乐hideBottomNav(){
bottomNav.visibility=View.GONE
}
  private fun setupNav() {
    val navController = findNavController(R.id.nav_host_fragment)
    findViewById<BottomNavigationView>(R.id.bottomNav)
        .setupWithNavController(navController)

    navController.addOnDestinationChangedListener { _, destination, _ ->
        when (destination.id) {
            R.id.mainFragment -> showBottomNav()
            R.id.mineFragment -> showBottomNav()
            else -> hideBottomNav()
        }
    }
}

private fun showBottomNav() {
    bottomNav.visibility = View.VISIBLE

}

private fun hideBottomNav() {
    bottomNav.visibility = View.GONE

}