Two different menus for Top App Bar and Bottom App bar with Navigation Components

后端 未结 2 1670
情书的邮戳
情书的邮戳 2021-01-12 14:53

I was trying out Android Navigation Architecture Component and was also looking into Material design guidelines. I really got inspired by the design below:

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 15:51

    Just use onCreateOptionsMenu() for the Toolbar as usual: (Kotlin)

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.menu_first, menu)
            return super.onCreateOptionsMenu(menu)
        }
    

    Then declare the Toolbar inside onCreate() and use setSupportActionBar():

    val toolbar = findViewById(R.id.myToolbar)
    setSupportActionBar(toolbar)
    

    And after that, replaceMenu() will do the trick: (Inside onCreate())

    val bottomBar = findViewById(R.id.bottomAppBar)
    bottomBar.replaceMenu(R.menu.menu_main)
    

    Note that if you wanted to use BottomSheetFragment for the NavigationView opening, you'll need setSupportActionBar in order to set menus for the BottomAppBar and I couldn't still find a way to fix this.

提交回复
热议问题