删除底部导航栏时,底部空白,并且应用栏覆盖布局

时间:2020-01-12 16:58:24

标签: android android-layout android-architecture-navigation android-bottomnavigationview

我对此进行了很多研究,但找不到解决方案,我无法弄清楚出了什么问题。我正在使用导航组件库和单个活动。当用户注销时,将用户带到登录名,底部的导航被删除,它留下了一个空白空间,就像整个布局向上移了一点(img 1)。然后,当您登录时,设计看起来像以前一样向上移动,但是显示了底部导航视图并显示了应用栏(img 2)。真是奇怪,我无法弄清楚哪里弄错了。应用程序流程如下:当用户打开应用程序时,将其带到HomeFragment,如果他已经登录,则访问homeFragment,否则,如果他未登录,则进行家庭碎片检查,然后带他登录fragment。但是,当用户关闭应用程序并重新启动它时,它显示良好;或者,如果用户已经登录并打开该应用程序,则它显示得很好。发生错误:当用户注销并进入登录屏幕时,登录后它仍然存在,直到应用程序重新启动。很抱歉那篇文章的丑陋:-\ enter image description here

enter image description here

以下是“活动”代码,当您进入登录屏幕时,该代码会删除底部栏视图和应用程序栏:

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()
            R.id.chatLogFragment -> hideBottomNavigation()
            else -> showBottomNavigation()
        }
    }

    // This creates a link between the bottomNavigationView and the navigation component (main_navigation_graph.xml)
    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 && alpha == 1f) {
            animate()
                    .alpha(0f)
                    .withEndAction { visibility = View.GONE }
                    .duration = 200
        }
    }
}

private fun showBottomNavigation() {
    // bottom_navigation is BottomNavigationView
    with(bottom_nav) {
        visibility = View.VISIBLE
        animate()
                .alpha(1f)
                .duration = 200
    }
}

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

这是MainActivity的xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<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" />
  </androidx.constraintlayout.widget.ConstraintLayout>

这是Login片段代码的例外,他的Layout的高度和宽度设置为match_parent:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)


    //Remove ActionBar
    val fragmentActivity = activity as AppCompatActivity
    fragmentActivity.supportActionBar?.hide()

    //Remove status bar
    fragmentActivity.window.decorView.systemUiVisibility =
            (View.SYSTEM_UI_FLAG_IMMERSIVE
                    // Set the content to appear under the system bars so that the
                    // content doesn't resize when the system bars hide and show.
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // Hide the nav bar and status bar
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN)

这里是HomeFragment的例外:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setHasOptionsMenu(true)
    if (FirebaseAuth.getInstance().uid == null) {            
NavHostFragment.findNavController(this).navigate(R.id.action_homeFragment_to_loginFragment)
    }
}
.....
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    activity!!.title = "Chapperone"

    val fragmentActivity = activity as AppCompatActivity
    fragmentActivity.supportActionBar?.show()
    fragmentActivity.supportActionBar?.title = "Chapperone"
    fragmentActivity.supportActionBar?.setDisplayHomeAsUpEnabled(false)
}

这里是导航图xml中的除外:

<?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/main_navigation_graph"
app:startDestination="@id/homeFragment">

<fragment
    android:id="@+id/homeFragment"
    android:name="com.gochapperone.Chapperone.HomeFragment"
    android:label="fragment_home"
    tools:layout="@layout/blank_fragment" >
    <action
        android:id="@+id/action_homeFragment_to_loginFragment"
        app:destination="@id/loginFragment"
        app:popUpTo="@id/homeFragment"
        app:popUpToInclusive="true" />
    <action
        android:id="@+id/action_homeFragment_to_editUserProfileFragment"
        app:destination="@id/editUserProfileFragment" />
    <action
        android:id="@+id/action_homeFragment_to_tripDetailsFragment"
        app:destination="@id/tripDetailsFragment" />
    <action
        android:id="@+id/action_homeFragment_to_editTripFragment"
        app:destination="@id/editTripFragment" />        
</fragment>
<fragment
    android:id="@+id/helpFragment"
    android:name="com.gochapperone.chapperone.ui.settings.HelpFragment"
    android:label="help"
    tools:layout="@layout/fragment_help"/>

0 个答案:

没有答案