How to set menu to Toolbar in Android

后端 未结 14 1488
轻奢々
轻奢々 2020-11-29 20:17

I want use ToolBar instead of ActionBar, but don\'t show me menu in toolbar!!! i want set menu such as Refresh or Setting<

14条回答
  •  佛祖请我去吃肉
    2020-11-29 20:36

    You can still use the answer provided using Toolbar.inflateMenu even while using setSupportActionBar(toolbar).

    I had a scenario where I had to move toolbar setup functionality into a separate class outside of activity which didn't by-itself know of the event onCreateOptionsMenu.

    So, to implement this, all I had to do was wait for Toolbar to be drawn before calling inflateMenu by doing the following:

    toolbar.post {
        toolbar.inflateMenu(R.menu.my_menu)
    }
    

    Might not be considered very clean but still gets the job done.

提交回复
热议问题