安卓工具栏菜单未显示

36

我想在工具栏中添加一个菜单。

我的ActivityonCreateOptionsMenu方法被调用,但没有菜单出现。

这是来自菜单文件夹的dashboard.xml:

<?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>

注意: 此菜单的图标颜色比操作栏的背景颜色更暗,因此应该可见。

在 Activity 中填充菜单:

public class DashboardActivity extends ActionBarActivity {

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

    return true;
}

应用程序的主题:

<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>

为什么调用了 onCreateOptionsMenu,但菜单没有出现。我正在使用 appcompat-v7:21.0.3
编辑:
    @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);
    }

尝试在你的xml项中添加android:orderInCategory="100" - Skizo-ozᴉʞS ツ
1
你的Activity布局长什么样?它是否包含一个Toolbar视图,并且你是否通过setSupportActionBar(toolbar)将其设置到活动中? - npace
我在从活动布局文件中膨胀工具栏后,使用 setSupportActionBar(toolbar)。 - Procurares
Joan,orderInCategory 没有帮助。 - Procurares
尝试将return true;替换为return(super.onCreateOptionsMenu(menu)); - Skizo-ozᴉʞS ツ
22个回答

45

我也遇到了同样的问题,但实际错误是,我忘记在Java活动中引入工具栏。

AppCompactActivity下,在on create方法中按ID定义您的工具栏,并调用setSupportActionBar(ToolBar);

以下是示例:

public class secondActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.showOverflowMenu();

2
翻译:没错,我也遇到了同样的问题。我按照你说的做了,问题解决了。 - cimenmus
我已经设置好了一切,但是没有使用 setSupportActionBar(ToolBar);介绍 setSupportActionBar 后,操作按钮才出现。 - Antroid
谢谢你,你帮我省了好几个小时的时间 :) - Nicks

32

我不确定为什么,但当我把与菜单膨胀相关的所有内容放在onPrepareOptionsMenu方法中时,一切都正常工作。

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

    return super.onCreateOptionsMenu(menu);
}

它确实起作用,但是为什么呢?inflate()的目的是什么?官方文档甚至都没有提及这一点。 - Jan Šimek
可能只是重叠颜色的问题。如果您的工具栏是黑色的,而菜单文本也是黑色的,那么您实际上看不到它。即使溢出菜单图标也是黑色的:D(在我的情况下发生了这种情况)。更进一步的沮丧是,如果只有一个或两个菜单选项,甚至菜单按钮似乎也无法工作(因为菜单已经存在于工具栏中)。 - Vinay Bhargav
2
每次点击时都会创建它,因此请使用以下代码:@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu);return true;} - Adnan Ali
2
你真的会调用 super.onCreateOptionsMenu(menu) 而不是 super.onPrepareOptionsMenu(menu) 吗? - kingston
1
最后应该返回“true”:https://developer.android.com/guide/topics/ui/menus。此外,建议重写onCreateOptionsMenu而不是onPrepareOptionsMenu。 - RandomEngy
显示剩余2条评论

11

尝试以下内容:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      tools:context="com.example.lolipoptest.MainActivity" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
</menu>

并且 Java 代码为:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

请检查此链接:https://dev59.com/XF8d5IYBdhLWcg3wmDKf - Akash

8

你的仪表板布局中是否有工具栏?在你的活动中调用setSupportActionBar(toolbar)。对于使用工具栏的活动,请使用Theme.AppCompat.NoActionBar主题。

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;

public class DashboardActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setHomeButtonEnabled(true);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

检查你的样式。

<resources>

<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme" parent="AppTheme.Base">

</style>

<style name="ToolbarTheme" parent="AppTheme" >
</style>

<color name="light">#FFBB33</color>
<color name="colorPrimary">#FFBB33</color>
<color name="textColorPrimary">#FFBB33</color>
<color name="colorPrimaryDark">#FF8800</color>
<color name="colorAccent">#ff9977</color>
<color name="white">#ffffff</color>

</resources>

检查您的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:titleMarginStart="20dp"
    android:paddingRight="10dp"
    android:background="@color/colorPrimaryDark"
    app:theme="@style/ToolbarTheme" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:text="Hello Toolbar" />

</LinearLayout>

在清单文件中的活动中添加主题。
android:theme="@style/AppTheme"

并非完全正确,如果未出现工具栏且使用了正确的主题,则该库会添加工具栏。 - pablisco

8

如果您正在从片段中膨胀菜单,例如通过覆盖onCreateOptionsMenu方法,请确保在片段的onCreateView中调用setHasOptionsMenu(true)


6
您需要在活动的onCreateOptionsMenu中填充菜单:
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mymenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

最后应该返回“true”:https://developer.android.com/guide/topics/ui/menus - RandomEngy

3
确保工具栏初始化在添加XML布局文件之后进行,示例如下:
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);

1

这个异常信息是:“此 Activity 已经有一个由窗口装饰提供的操作栏。请勿请求 Window.FEATURE_ACTION_BAR,并在您的主题中将 windowActionBar 设置为 false,以使用 Toolbar 替代。” - Procurares
这句话不太通顺,yourapp:应该是什么意思?命名空间app:是正确的。 - lasec0203

1

如果您正在使用 Toolbar,则需要在 onCreate 中将其设置为支持操作栏:

setSupportActionBar(toolbar);


1
在我的情况下,问题非常简单。我的工具栏是AppBarLayout的子项,但由于某种原因,当我使用约束布局设置工具栏时,工具栏的layout_width xml参数被设置为0dp。因此,工具栏存在,但是不可见... (>_<)。
所以,如果以上方法都没用,请检查“layout_width”和“layout_height”参数。
希望这能节省某人的时间 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接