android - 导航后不显示底部导航视图
问题描述
在我的项目中,我添加了navhost
片段和BottomNavigationView
我的activity_main,导航时一切似乎都很好,但是当我单击工具栏选项时,我导航到另一个页面,但我仍然看到底部导航视图,我怎么看不到底部导航视图?我添加了导航 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>
解决方案
我一直在为这个问题苦苦挣扎,最后我采用了这种方法:在你的 MainActivity
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,如果前一个fragment和当前fragment都具有bottom_nav的相同可见性( View.Gone 或 View.Visible ),那些 if 语句会阻止动画运行
然后你可以定义两个函数: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 添加到 BOTTOM_NAV_VISIBLE_FRAGMENTS 列表中,如果您想更改隐藏/显示动画,只需将其添加到 show/hideBottomNavigationView 函数。
希望有帮助:)
推荐阅读
- gams-math - 如何在游戏中的单个方程中设置单个变量的下限和上限
- node.js - 为什么 brew 说 nodejs 已安装?
- reactjs - onChange using Formik Validation
- android - 在 Android Studio 上获取“无法确定标签的类型”
- java - 项目从一台计算机移动到另一台计算机后 ImageIO.read() 不起作用
- c - 在C中将数字转换为二进制和十六进制
- python - How to compare two tables and identify a specific type of row to be returned?
- python - Is there some way to find the intersect in multiple file names between multiple CSV headers?
- r - How to create a stand alone legend in R
- php - mb_detect_encoding does not detect SJIS-mac?