使用噴氣背包導航,具有多個片段的單個活動。通過導航圖中定義的操作從主片段導航到另一個片段后,漢堡選單圖示保持不變,它沒有變為后退箭頭按鈕。
如何將此漢堡選單圖示更改為后退箭頭按鈕?單擊時,它應該回傳到主片段。
在 Android Studio 中創建一個新專案并選擇 Navigation Drawer Activity 作為模板將設定具有上述 3 個片段的單個 Activity。
uj5u.com熱心網友回復:
只需將其添加到您的根活動中即可。
if (supportFragmentManager.backStackEntryCount > 0) {
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener {
if (supportFragmentManager.backStackEntryCount > 0) {
super.onBackPressed()
} else {
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
drawerLayout.openDrawer(GravityCompat.START)
}
}
} else {
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
}
uj5u.com熱心網友回復:
在你的活動中試試這個
NavigationView nav_view = (NavigationView) findViewById(R.id.nav_view);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
setupActionBarWithNavController(this, navController,appBarConfiguration);
NavigationUI.setupWithNavController(nav_view, navController);
NavigationUI.setupActionBarWithNavController(this, navController);
NavigationUI.setupActionBarWithNavController(this, navController, drawer);
uj5u.com熱心網友回復:
我在 AppBarConfiguration 中設定了多個片段。
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_main, R.id.nav_detail
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
由于細節片段是在 AppBarConfiguration 中設定的,這就是為什么當從主片段導航到細節片段時,漢堡選單圖示保持不變,因為細節片段被設定為抽屜布局之一。
從 AppBarConfiguration 中洗掉 R.id.nav_detail 后,導航到詳細資訊片段,漢堡選單圖示將自動更改為帶有 DrawerArrowDrawable 影片的后退箭頭圖示。
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_main
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460284.html
上一篇:為什么打開片段我需要雙擊選單項?
