Android BottomNavigationView删除/隐藏后留空白

问题描述 投票:0回答:2

enter image description here

[当用户未通过身份验证时,它将转到登录片段,如果在通过身份验证时,它将进入本地片段。当未经身份验证的用户打开应用程序时,bottomNavView应该被隐藏,但是它留下了空白。

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()
}

这是for主要活动的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" />
android bottomnavigationview android-architecture-navigation
2个回答
0
投票

只需更改为:

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

        }
    }
}

0
投票

更改为

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

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.