Android 导航后不显示底部导航视图
在我的项目中,我将Android 导航后不显示底部导航视图,android,kotlin,navigation,android-jetpack-navigation,Android,Kotlin,Navigation,Android Jetpack Navigation,在我的项目中,我将navhost片段和BottomNavigationView添加到“我的活动”主视图中,导航时一切似乎都很好,但当我单击工具栏选项时,我导航到了另一个页面,但我仍然看到底部导航视图,我怎么能看不到底部导航视图?我添加了导航xml来导航所有页面,并在我的资源文件上创建了底部导航菜单。唯一的问题是,当我导航到底部导航页面中的另一个页面时,仍然会看到底部导航视图 <androidx.appcompat.widget.Toolbar android:id="@+
navhost
片段和BottomNavigationView
添加到“我的活动”主视图中,导航时一切似乎都很好,但当我单击工具栏选项时,我导航到了另一个页面,但我仍然看到底部导航视图,我怎么能看不到底部导航视图?我添加了导航xml来导航所有页面,并在我的资源文件上创建了底部导航菜单。唯一的问题是,当我导航到底部导航页面中的另一个页面时,仍然会看到底部导航视图
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/text_some"
android:fontFamily="@font/lobster_two"
android:text="@string/text_some"
android:textColor="@color/colorSecondary"
android:textSize="24sp" />
</androidx.appcompat.widget.Toolbar>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/app_toolbar"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/bottom_navigation_graph" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_navigation_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:showAsAction="always|withText"
app:itemIconSize="22dp"
app:itemIconTint="@drawable/bottom_nav_color"
app:itemTextColor="@drawable/bottom_nav_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circle_view_image"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_marginBottom="25dp"
android:elevation="99dp"
android:src="@drawable/image"
app:layout_constraintBottom_toBottomOf="@+id/bottom_nav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
单击工具栏选项时,我导航到另一个页面
我不确定这意味着什么,但我假设这个工具栏图标单击会启动一个全局操作,该操作将导航到另一个片段,您希望在其中隐藏底部导航
要在某些片段中隐藏底部导航,请在main活动中执行此操作:
navController.addOnDestinationChangedListener { _, nd: NavDestination, _->
if (nd.id == R.id.fragmentWithNoBottomNav){
bottom_nav.visibility = View.GONE
} else {
bottom_nav.visibility = View.VISIBLE
}
我为这个问题挣扎了一段时间,最后我采用了这种方法:
在你的主要活动中
companion object {
private val BOTTOM_NAV_VISIBLE_FRAGMENTS =
listOf(
R.id.Fragment1,
R.id.Fragment2,
R.id.Fragment3,
R.id.Fragment4,
)
private const val EXIT_DURATION = 250L
private const val ENTER_DURATION = 250L
}
为BottomNavigationView可见性定义一个getter函数
private val isBottomNavigationViewVisible: Boolean
get() = bottom_nav.visibility == View.VISIBLE
然后在onCreate函数中:
navController.addOnDestinationChangedListener { _, destination, b ->
invalidateOptionsMenu()
when (destination.id) {
in BOTTOM_NAV_VISIBLE_FRAGMENTS -> {
if (!isBottomNavigationViewVisible) showBottomNavigation()
}
else -> {
if (isBottomNavigationViewVisible) hideBottomNavigation()
}
}
}
假设您从fragment1切换到fragment2,两者都将bottom_nav.visibility设置为true,即if语句
如果上一个片段和当前片段对底部导航具有相同的可见性(都是View.Gone或View.Visible),则阻止运行动画
然后您可以定义两个函数:showBottomNavigationView和hideBottomNavigationView,这样您就可以设置动画和。。。如果你愿意
例如:
private fun hideBottomNavigation() {
with(bottom_nav) {
if (visibility == View.VISIBLE && alpha == 1f) {
animate()
.alpha(0f)
.withEndAction { visibility = View.GONE }
.duration = EXIT_DURATION
}
}
}
private fun showBottomNavigation() {
with(bottom_nav) {
visibility = View.VISIBLE
animate()
.alpha(1f)
.duration = ENTER_DURATION
}
}
使用这种方法,如果您想在另一个片段中显示bottomNavigationView,只需将该片段ID添加到底部的“导航可见”片段列表中,
如果要更改隐藏/显示动画,只需将其添加到显示/隐藏动画导航视图功能中即可
希望有帮助:)