Android ToolBar inflateMenu 不生效

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"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ContentActivity">
<item
    android:id="@+id/action_login"
    android:icon="@mipmap/ic_drawer_am"
    android:title="Login"
    app:showAsAction="always" />
<item
    android:id="@+id/action_settiong"
    android:orderInCategory="100"
    android:title="action_settings"
    app:showAsAction="never" />
<item
    android:id="@+id/action_night"
    android:orderInCategory="100"
    android:title="Night"
    app:showAsAction="never"></item>
</menu>

然而,发现不调用ToolBar本身的接口,调用Activity本身创建menu的接口却可以显示!!

//设置ToolBar的选项
@Override
public 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!!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,可以得出在使用iframe添加toolbar时,可能会出现不生效的情况。其中引用\[1\]提到了通过在PDF地址后面添加参数来隐藏toolbar,但是这种方法可能不适用于所有的PDF查看器。而引用\[2\]提到了在项目中使用iframe来显示PDF,并且可以通过在地址栏中添加参数来显示toolbar,但是具体的实现方式可能需要根据具体的项目和PDF查看器来进行调整。 如果你在使用iframe添加toolbar时遇到了问题,可以尝试以下几个解决方案: 1. 确保你使用的PDF查看器支持通过参数来显示toolbar。不同的PDF查看器可能有不同的参数设置方式,你可以查阅相关文档或者官方网站来获取更多信息。 2. 检查你的代码是否正确,确保你正确地添加了参数并且参数的格式正确。有时候一个小错误可能导致参数不生效。 3. 尝试使用其他的PDF查看器或者工具来显示PDF,并查看是否可以通过参数来显示toolbar。有时候不同的工具可能有不同的实现方式。 如果以上方法都没有解决你的问题,你可以尝试使用其他的解决方案或者咨询相关的技术支持人员来获取更多帮助。 #### 引用[.reference_title] - *1* *2* *3* [IE 11+中打印直接PDF,chrome打印pdf ,隐藏头部工具栏,hide toolbar](https://blog.csdn.net/weixin_30758821/article/details/98082615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值