问题
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()
}
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" />
回答1:
Change to
private fun hideBottomNavigation() {
// bottom_navigation is BottomNavigationView
with(bottom_nav) {
if (visibility == View.VISIBLE) {
visibility = View.GONE
}
}
}
回答2:
Just change to:
private fun hideBottomNavigation() {
// bottom_navigation is BottomNavigationView
with(bottom_nav) {
if (visibility == View.VISIBLE) {
visibility = View.GONE
}
}
}
来源:https://stackoverflow.com/questions/58631212/android-bottomnavigationview-leave-blank-space-after-being-removed-hidden