android – 导航组件,控制何时显示汉堡包或后退图标

android – 导航组件,控制何时显示汉堡包或后退图标,第1张

概述我有以下活动 class MainActivity : AppCompatActivity() {private lateinit var drawerLayout: androidx.drawerlayout.widget.DrawerLayoutoverride fun onCreate(savedInstanceState: Bundle?) { super.onCreate 我有以下活动
class MainActivity : AppCompatActivity() {private lateinit var drawerLayout: androIDx.drawerlayout.Widget.DrawerLayoutoverrIDe fun onCreate(savedInstanceState: Bundle?) {    super.onCreate(savedInstanceState)    setContentVIEw(R.layout.main_activity)    drawerLayout = drawer_layout    val navController = Navigation.findNavController(this,R.ID.fragment_main_navHost)    setSupportActionbar(toolbar)    NavigationUI.setupActionbarWithNavController(this,navController,drawerLayout)    navVIEw_main.setupWithNavController(navController)}overrIDe fun onSupportNavigateUp(): Boolean {    return NavigationUI.navigateUp(drawerLayout,Navigation.findNavController(this,R.ID.fragment_main_navHost))}overrIDe fun onBackpressed() {    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {        drawerLayout.closeDrawer(GravityCompat.START)    } else {        super.onBackpressed()    }}

你可以看到它与导航图相关联,我正在使用导航抽屉.当我浏览抽屉中的项目时,我想保留汉堡图标,只有当我点击片段或d出窗口中的项目时才将其更改为上/后按钮,并确保系统的行为反映了什么用户期望基于显示的图标.那可能吗

解决方法 按照以下步骤 *** 作

1.绑定你的NavigationView with NavigationUI

NavigationUI.setupWithNavController(nav_vIEw,hostFragment.navController)

2.绑定ActionBar With NavController

NavigationUI.setupActionbarWithNavController(this@NavActivity,hostFragment.navController)

3.绑定ActionBar and DrawerLayout With NavController

NavigationUI.setupActionbarWithNavController(this@NavActivity,hostFragment.navController,drawer_layout)

4.覆盖您活动中的onSupportNavigateUp()

overrIDe fun onSupportNavigateUp(): Boolean {    return NavigationUI.navigateUp(drawer_layout,hostFragment.navController)               || super.onSupportNavigateUp()}

样品:

class NavActivity : AppCompatActivity(),NavigationVIEw.OnNavigationItemSelectedListener {    lateinit var hostFragment: NavHostFragment    overrIDe fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentVIEw(R.layout.activity_nav)        setSupportActionbar(toolbar)        fab.setonClickListener { vIEw ->            Snackbar.make(vIEw,"Replace with your own action",Snackbar.LENGTH_LONG)                    .setAction("Action",null).show()        }        val toggle = ActionbarDrawerToggle(                this,drawer_layout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close)        drawer_layout.addDrawerListener(toggle)        toggle.syncState()        nav_vIEw.setNavigationItemSelectedListener(this)        hostFragment = supportFragmentManager.findFragmentByID(R.ID.my_nav_host_fragment) as NavHostFragment            NavigationUI.setupWithNavController(nav_vIEw,hostFragment.navController)            NavigationUI.setupActionbarWithNavController(this@NavActivity,drawer_layout)        }    overrIDe fun onBackpressed() {        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {            drawer_layout.closeDrawer(GravityCompat.START)        } else {            super.onBackpressed()        }    }    overrIDe fun onSupportNavigateUp(): Boolean {        return NavigationUI.navigateUp(drawer_layout,hostFragment.navController) || super.onSupportNavigateUp()    }    overrIDe fun onCreateOptionsMenu(menu: Menu): Boolean {        // Inflate the menu; this adds items to the action bar if it is present.        menuInflater.inflate(R.menu.nav,menu)        return true    }    overrIDe fun onoptionsItemSelected(item: MenuItem): Boolean {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button,so long        // as you specify a parent activity in AndroIDManifest.xml.        when (item.itemID) {            R.ID.action_settings -> return true            else -> return super.onoptionsItemSelected(item)        }    }    overrIDe fun onNavigationItemSelected(item: MenuItem): Boolean {        // Handle navigation vIEw item clicks here.        drawer_layout.closeDrawer(GravityCompat.START)        return true    }}

产量

家庭片段:

片段二:

片段树:

总结

以上是内存溢出为你收集整理的android – 导航组件,控制何时显示汉堡包或后退图标全部内容,希望文章能够帮你解决android – 导航组件,控制何时显示汉堡包或后退图标所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存