Android BottomNavigationView 在 removed/hidden 之后留空 space

Android BottomNavigationView leave blank space after being removed/hidden

当用户未通过身份验证时,它将转到登录片段,如果他通过了身份验证,它将转到主页片段。当未经授权的用户打开应用程序时,bottomNavView 应该被隐藏,但它留下了一个空白 space。

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val navigationController = findNavController(R.id.nav_host_fragment)
    val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)

    findNavController(R.id.nav_host_fragment).addOnNavigatedListener { _, destination ->
        when (destination.id) {
            R.id.register1Fragment -> hideBottomNavigation()
            R.id.register2Fragment -> hideBottomNavigation()
            R.id.loginFragment -> hideBottomNavigation()
            else -> showBottomNavigation()
        }
    }

    NavigationUI.setupWithNavController(bottomNavigationView, navigationController)

    // Sets up the Toolbar actions (like Back Button) to be managed by the Navigation Component
    NavigationUI.setupActionBarWithNavController(this, navigationController)
}

private fun hideBottomNavigation() {
    // bottom_navigation is BottomNavigationView
    with(bottom_nav) {
        if (visibility == View.VISIBLE) {
            visibility = View.INVISIBLE

        }
    }
}

private fun showBottomNavigation() {
    // bottom_navigation is BottomNavigationView
    with(bottom_nav) {
        visibility = View.VISIBLE
    }
}

override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
}

这是主 activity 的 xml。这显示了 bottomNavView 是如何实现的。根viewGroup是一个ConstraintLayout,高度和宽度设置为match_parent。我很难弄清楚真正的问题出在哪里。

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/main_navigation_graph"
    app:defaultNavHost="true"

    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:labelVisibilityMode="unlabeled"
    android:background="@color/colorPrimary"
    app:itemBackground="@color/bottom_nav_state"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/menu_bottom_nav" />

只需更改为:

private fun hideBottomNavigation() {
    // bottom_navigation is BottomNavigationView
    with(bottom_nav) {
        if (visibility == View.VISIBLE) {
            visibility = View.GONE

        }
    }
}

改为

private fun hideBottomNavigation() {
    // bottom_navigation is BottomNavigationView
    with(bottom_nav) {
        if (visibility == View.VISIBLE) {
            visibility = View.GONE

        }
    }
}