我正在尝试在我的应用程序中使用 android-navigation lib,并且按照教程中的说明进行操作。我只想在我的应用程序中使用一个活动。我对一个问题感到困惑。一些不想要 BottomNavigationView 的片段,我该如何隐藏它。这是我的 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
<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 设置为可见。但这很奇怪。任何可以帮助我的人,在此先感谢。
原文由 Freddy 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以在活动的 onCreate 中执行类似的操作。 When ever an item in the nav bar is selected it will show or hide the nav based on the fragment id’s.