工具栏不显示菜单按钮

Toolbar Won't Show Menu Button

我尝试了在 Android Studio 更新中制作菜单按钮的不同方法,但无法显示菜单按钮。

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

我在 Activity 中添加了这段代码,并制作了一个 menu.xml 文件,其中包含:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/expanded_menu"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_menu"
        app:showAsAction="always"
        android:title="button">
    </item>
    <item android:id="@+id/credits"
        android:title="Credits"
        android:orderInCategory="100"
        />
     <item android:title="Settings"
        android:id="@+id/settings"
        android:orderInCategory="101"
         />
</menu>

请问如何让旧版本Android自动添加的三个方块出现?

这是应用程序的屏幕截图,在android studio 的早期版本中,右上角有三个点。 目前他们不在那里,我的问题是如何添加菜单按钮。

编辑:也在写 app:showAsAction="always" android studio 时将所有行标记为红色,直到我写了“=”符号,也许这个错误与某些事情有关? 也可能我在制作 items/toolbar 时错过了一步,但我不知道我是否错过了什么。

我相信您是在谈论溢出按钮(标记为“3”):

当没有足够的空间来显示选项菜单中的所有项目时,或者当您指定特定项目不作为操作显示时,会出现此按钮。这是由 app:showAsAction 属性处理的。您可以应用五个不同的值,描述如下:

  • ifRoom : Only place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom", the items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu.

  • withText : Also include the title text (defined by android:title) with the action item. You can include this value along with one of the others as a flag set, by separating them with a pipe |.

  • never : Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.

  • always : Always place this item in the app bar. Avoid using this unless it's critical that the item always appear in the action bar. Setting multiple items to always appear as action items can result in them overlapping with other UI in the app bar.

  • collapseActionView : The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible. Introduced in API Level 14.

因此,如果您只想显示溢出按钮,您应该为所有菜单项设置app:showAsAction="never"

不过,选项菜单的典型结构是始终显示第一项,并指定要显示的其余项 "ifRoom"