【问题标题】:Main Menu Adding Buttons Not appearing主菜单添加按钮未出现
【发布时间】:2015-10-27 05:03:54
【问题描述】:

所以根据我使用的 android studio 版本,它带有不同的模板。一个带有一个 main_menu.xml 文件,它允许您将项目(按钮)添加到您的主目录。好吧,最近我没有附带main_menu.xml的模板,所以我手动添加了.xml文件并添加了我的代码:

<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=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:title="Hello" />

    <item
        android:id="@+id/action_mainMenu2"
        android:icon="@drawable/ic_add_circle_white_24dp"
        android:orderInCategory="10"
        android:title="Main Menu"
        app:showAsAction="ifRoom|withText" /> />

</menu>

我的 mainactivity.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.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_mainMenu2:
            startActivity(new Intent(MainActivity.this, Main2Activity.class));

        case R.id.action_mainMenu:
            startActivity(new Intent(MainActivity.this, Main2Activity.class));
            return true;
        case R.id.action_settings:

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

遗憾的是菜单不会添加任何项目

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    确保您已覆盖 onCreateOptionsMenu。参考文档:

    初始化 Activity 的标准选项菜单的内容。您应该将菜单项放入菜单中。这只调用一次,第一次显示选项菜单。默认实现使用标准系统菜单项填充菜单。

    以下内容就足够了:

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

    【讨论】:

    • 我为你投票,但它不起作用。我有 oncreateoption,但我的菜单右上角有 3 个点,它只是将我的项目显示为选项卡
    • @SPete 那是因为你使用showAsAction 作为alwayswithRoom。请改用never,如果您不希望它们那样显示并且希望它们在单击 3 个点(溢出菜单图标)后显示。
    【解决方案2】:

    您也必须覆盖 onCreateOptionsMenu(Menu menu) 生命周期方法。详情请见documentation

    【讨论】:

    • 我已为您投票,但它不起作用。我有 oncreateoption,但我的菜单右上角有 3 个点,它只是将我的项目显示为一个选项卡。
    【解决方案3】:

    删除android studio推荐的"xmlns:app="http://schemas.android.com/apk/res-auto"是错误的。删除它并使用android而不是app。

    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="Hello" />
    
    <item
        android:id="@+id/action_mainMenu2"
        android:icon="@drawable/ic_add_circle_white_24dp"
        android:orderInCategory="10"
        android:title="Main Menu"
        android:showAsAction="ifRoom|withText" /> />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多