Android BottomNavigationView被删除/隐藏后保留空白 [英] Android BottomNavigationView leave blank space after being removed/hidden

查看:88
本文介绍了Android BottomNavigationView被删除/隐藏后保留空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户未通过身份验证,则转到登录"片段;如果用户通过身份验证,则转到主页"片段.当未经身份验证的用户打开应用程序时,bottomNavView应该被隐藏,但是它留下了空白.

When user in not authenticate, it goes to Login fragment, if he's authenticate it goes to home fragment. When unauthenticated user open the app, the bottomNavView should be hidden, it is but it leaves a blank 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()
}

这是for主要活动的xml.那显示了bottomNavView是如何实现的.根viewGroup是ConstraintLayout,高度和宽度设置为match_parent.我很难弄清楚真正的问题在哪里.

Here's the xml for the for main activity. That show how the bottomNavView is implemented. The root viewGroup is a ConstraintLayout, height and width are set to match_parent. I had a hard time figuring out where the real problem is.

<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

        }
    }
}

这篇关于Android BottomNavigationView被删除/隐藏后保留空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆