如何在android-navigation lib上隐藏BottomNavigationView [英] how to hide BottomNavigationView on android-navigation lib
问题描述
我正在尝试在我的 APP 中使用 android-navigation lib,并且按照教程中的说明进行操作.我只想在我的 APP 中使用一项活动.我对一个问题感到困惑.一些不想要BottomNavigationView 的片段,我该如何隐藏它.这是我的 main_layout
I am trying to use android-navigation lib in my APP and I do the things as the tutorial said. I just wanna use one single activity in my APP. I am confused about one question. some fragments that just don't want the BottomNavigationView, how can I hide it. here is my main_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:popupTheme="@style/AppTheme.PopupOverlay" >
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/carkeeper_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/menu_bottom_nav"
app:itemTextColor="@color/bottom_nav_title_color_selector"
app:itemIconSize="@dimen/x40"
app:menu="@menu/menu_main_bottom_nav"
app:labelVisibilityMode="labeled">
</com.google.android.material.bottomnavigation.BottomNavigationView>
这是我的主要活动
class MainActivity : BaseActivity() {
private lateinit var navController: NavController
private lateinit var bottomNavigationView: BottomNavigationView
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.home_activity)
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph, null)
setSupportActionBar(findViewById(R.id.toolbar))
bottomNavigationView = findViewById(R.id.menu_bottom_nav)
bottomNavigationView.setupWithNavController(navController)
bottomNavigationView.itemIconTintList = null
}}
然后是 navigaton_graph
then the navigaton_graph
<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/carkeeper_navigation"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.saicfinance.carkeeper.func.main.MainFragment"
android:label="MainFragment"
tools:layout="@layout/home_fragment">
</fragment>
<fragment
android:id="@+id/mineFragment"
android:name="com.saicfinance.carkeeper.func.mine.MineFragment"
android:label="@string/mine_title"
tools:layout="@layout/mine_fragment" >
<action android:id="@+id/action_mine_fragment_to_setting_fragment"
app:destination="@id/settingFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"/>
</fragment>
<fragment
android:id="@+id/settingFragment"
android:name="com.freddy.func.setting.SettingFragment"
android:label="setting_fragment"
tools:layout="@layout/setting_fragment" />
我知道我可以在导航到 settingFragment 时将 BottomNavigationView 设置为消失.然后在返回我的片段时设置 BottomNavigationView 可见.但这很奇怪.任何可以帮助我的人,提前致谢.
I know I can set BottomNavigationView gone when navigating to settingFragment. then set BottomNavigationView visible when back to mine fragment. But that is strange. anyone who can help me, thanks in advance.
推荐答案
您可以在活动的 onCreate 中执行类似的操作.当导航栏中的某个项目被选中时,它将根据片段 ID 显示或隐藏导航.
You could do something like this in your activity's onCreate. When ever an item in the nav bar is selected it will show or hide the nav based on the fragment id's.
private fun setupNav() {
val navController = findNavController(R.id.nav_host_fragment)
findViewById<BottomNavigationView>(R.id.bottomNav)
.setupWithNavController(navController)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.mainFragment -> showBottomNav()
R.id.mineFragment -> showBottomNav()
else -> hideBottomNav()
}
}
}
private fun showBottomNav() {
bottomNav.visibility = View.VISIBLE
}
private fun hideBottomNav() {
bottomNav.visibility = View.GONE
}
这篇关于如何在android-navigation lib上隐藏BottomNavigationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!