Android的工具栏不显示菜单 [英] Android toolbar menu is not showing

查看:182
本文介绍了Android的工具栏不显示菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想菜单添加到工具栏。 onCreateOptionsMenu 我的活动的方式被调用,但没有菜单出现。

这是dashboard.xml(从菜单文件夹)

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
      的xmlns:工具=htt​​p://schemas.android.com/tool​​s
      工具:上下文=com.app.android.ui.dashboard.DashboardActivity>

    <项目
        机器人:ID =@ + ID / action_scan_qr
        机器人:图标=@可绘制/ ic_drawer
        机器人:标题=@字符串/ menu_scan_qr
        应用程序:showAsAction =总是/>
< /菜单>
 

注:此菜单的图标比操作栏的背景颜色较暗,所以它应该是可见的。

在活动充气菜单:

 公共类DashboardActivity扩展ActionBarActivity {

@覆盖
公共布尔onCreateOptionsMenu(最后菜单菜单){
    。getMenuInflater()膨胀(R.menu.dashboard,菜单);

    返回true;
}
 

和主旋律应用程序:

 <样式名称=Theme.Application.Base父=Theme.AppCompat.Light>
        <项目名称=colorPrimary> @android:彩色/白< /项目>
        <项目名称=colorPrimaryDark> @android:彩色/白< /项目>
        <项目名称=机器人:windowNoTitle>真< /项目>
        <项目名称=windowActionBar>假< /项目>
        <项目名称=drawerArrowStyle> @风格/ Theme.Application.DrawerArrowStyle< /项目>
        <项目名称=机器人:textColorSecondary> @android:彩色/ darker_gray< /项目>
< /风格>
 

为什么 onCreateOptionsMenu 的调用,但并没有出现菜单。我使用 appcompat-V7:21.0.3

编辑:

  @覆盖
    保护无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(getContentViewId());

        工具栏=(栏)findViewById(R.id.tool_bar);
        如果(工具栏== NULL){
            抛出新的错误(找不到工具栏,你忘了将其添加到活动布局文件?);
        }

        setSupportActionBar(工具栏);
        getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
        getSupportActionBar()setHomeButtonEnabled(真)。
    }
 

解决方案

我不知道为什么,但是当我把一切相关的菜单上prepareOptionsMenu方法在膨胀,一切工作正常。

  @覆盖
在prepareOptionsMenu(最后菜单菜单){公共布尔
    。getMenuInflater()膨胀(R.menu.dashboard,菜单);

    返回super.onCreateOptionsMenu(菜单);
}
 

I'm trying to add a menu to the ToolBar. onCreateOptionsMenu method of my Activity is called, but no menu appears.

This is dashboard.xml (from menu folder)

<?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="com.app.android.ui.dashboard.DashboardActivity">

    <item
        android:id="@+id/action_scan_qr"
        android:icon="@drawable/ic_drawer"
        android:title="@string/menu_scan_qr"
        app:showAsAction="always" />
</menu>

NOTE: icon of this menu is darker than the background color of the action bar, so it should be visible.

Inflating menu in Activity:

public class DashboardActivity extends ActionBarActivity {

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.dashboard, menu);

    return true;
}

And the main theme for the application:

<style name="Theme.Application.Base" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@android:color/white</item>
        <item name="colorPrimaryDark">@android:color/white</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="drawerArrowStyle">@style/Theme.Application.DrawerArrowStyle</item>
        <item name="android:textColorSecondary">@android:color/darker_gray</item>
</style>

Why onCreateOptionsMenu is called but menu doesn't appears. I'm using appcompat-v7:21.0.3

EDIT:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        if (toolbar == null) {
            throw new Error("Can't find tool bar, did you forget to add it in Activity layout file?");
        }

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

解决方案

I'm not sure why, but when i place everything related menu inflating in onPrepareOptionsMenu method, everything works fine.

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.dashboard, menu);

    return super.onCreateOptionsMenu(menu);
}

这篇关于Android的工具栏不显示菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆