Android(安卓)ToolBar inflateMenu 不生效
16lz
2021-01-24
ToolBar inflateMenu 不生效
by seaicelin
转载请注明出处:http://blog.csdn.net/amd123456789/article/details/52474984
在使用ToolBar的时候,发现调用
Toolbar.inflateMenu(R.menu.toolbar_menu);
一直显示不出ToolBar右上角的图标。其中menu文件如下:
<?xml version="1.0" encoding="utf-8"?>
然而,发现不调用ToolBar本身的接口,调用Activity本身创建menu的接口却可以显示!!
//设置ToolBar的选项@Overridepublic boolean onCreateOptionsMenu(Menu menu) { //MenuInflater inflater = getMenuInflater(); //inflater.inflate(R.menu.toolbar_menu, menu); return super.onCreateOptionsMenu(menu);}
这个问题纠结,查了很久!总算找到答案了:
http://stackoverflow.com/questions/26511981/toolbar-inflatemenu-seems-to-do-nothing
If you are calling setSupportActionBar() you don’t need to use toolbar.inflateMenu() because the Toolbar is acting as your ActionBar. All menu related callbacks are via the default ones. The only time you need to call toolbar.inflateMenu() is when you are using the Toolbar as a standalone widget.
意思就是说,要想让Toolbar本身的inflateMenu生效,则必须删去这两句代码!!!
setSupportActionBar(toolbar);getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This is the answer!!
更多相关文章
- Ubuntu为安装的软件添加启动图标
- Android(安卓)中关于属性动画的一些思考,或许能为你解决一定的性
- Android(安卓)之ExpandableListView几个特殊的属性
- Android(安卓)volley封装实践其二
- 基于32bit系统编译Android(安卓)2.3
- Android(安卓)的系统属性(SystemProperties)设置分析
- Android(安卓)textview获取选中的内容
- android Studio 引用fresco 出现的问题
- Android(安卓)AIDL详解