在Android Studio中实现选项菜单

11

我该如何在我的安卓应用程序中实现选项菜单? 我尝试了来自安卓开发者网站的代码,但是我得到了错误,例如: 元素菜单必须声明。 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
    android:icon="@drawable/ic_help"
    android:title="@string/help" />
</menu>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lucavanraalte.test" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <activity android:name=".MainActivity" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

</manifest>

在此输入图像描述


你把它放错了位置,不要把它放在Manifest.xml文件中,而是放在你创建活动布局的(YourActivity).xml文件中。 - Strider
我无法确定这是否只是您在问题中格式化代码的方式,但任何菜单文件都需要在res文件夹的menus子目录中拥有自己的xml文件 - 它不应该是清单的一部分。 - PPartisan
我需要把它放在哪里,放什么?我需要创建一个新文件夹吗?请参见上面的图片。 - lucafj2j282j
1
在您的res(资源)文件夹中创建一个名为menu的文件夹。然后在该文件夹内创建一个XML文件,并将您的菜单代码放置其中。请查看下面的答案。 - Strider
5个回答

22
在你的Java代码中,添加这个onCreateOptionsMenu来显示选项菜单。
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); //your file name
        return super.onCreateOptionsMenu(menu);
    }

请保留您的 under res\menu\option_menu 文件夹。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
    android:icon="@drawable/ic_help"
    android:title="@string/help" />
</menu>

现在,如果你想设置onOptionsItemSelected,也就是为其设置onClick事件,你可以使用以下代码:

@Override
    public boolean onOptionsItemSelected(final MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.new_game:
                //your code
                // EX : call intent if you want to swich to other activity 
                return true;
            case R.id.help:
                //your code
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

问题已经解决了,但是我该如何添加选项呢?我的应用程序中不应该看到菜单标志吗? - lucafj2j282j
现在如果您遵循这段代码,您的选项已经添加了。尝试运行代码并再次通知。 - Amit Vaghela
弹出窗口的样式无法更改,这是系统默认设置。@Lucafraser - Amit Vaghela
好的,我的背景图像和按钮颜色为什么没有显示出来呢?按钮应该是黄色的,但现在却是白色的。背景图像也没有显示出来。 - lucafj2j282j
是的,为什么不呢?或者问另一个问题,以免其他用户对此问题感到困惑。 - Amit Vaghela
显示剩余6条评论

3

你应该使用onCreateOptionsMenu (Menu menu)

初始化活动标准选项菜单的内容。您应该将菜单项放置在菜单中。

这仅在首次显示选项菜单时调用一次。要每次显示菜单时更新菜单,请参见onPrepareOptionsMenu(Menu)。

需要在Activity类中覆盖的onCreateOptionsMenu(Menu menu)方法。这将创建菜单并返回布尔值。inflate从XML资源中膨胀一个菜单层次结构。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu); // set your file name
        return super.onCreateOptionsMenu(menu);
    }

您的 option_menu.xml 文件
    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item_First" 
          android:title="@string/item_First"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/save_menu" 
          android:title="@string/save"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/item_Second"
          android:title="@string/item_First"
          android:showAsAction="ifRoom"/>

</menu> 

请查看演示 Android选项菜单示例

1
你需要在 res 目录下创建一个名为 menu 的文件夹,并在该文件夹中创建名为 my_menu.xml 的文件。在该文件中编写以下内容:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
    android:icon="@drawable/ic_help"
    android:title="@string/help" />
</menu>

然后在您的Activity中执行以下操作:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

当我将第二部分添加到我的MainActivity.java时,我会遇到错误,例如“无法解析符号,例如Inflater”。 - lucafj2j282j

1
你需要在res->menu目录下创建一个名为menu.xml的文件,用于创建菜单。
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>

然后您需要使用以下代码从活动创建菜单。
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.help) {

       //do something
       return true;
    }
    if (id == R.id.new_game) {

       //do something
       return true;
    }
    return super.onOptionsItemSelected(item);
}

0
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Home");
    menu.add("Profile");
    menu.add("Settings");
    menu.add("Privacy Policy");
    menu.add("Terms of use");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getTitle()=="Home"){
        Toast.makeText(this, "Home option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Profile"){
        Toast.makeText(this, "Profile option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Settings"){
        Toast.makeText(this, "Settings option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Privacy Policy"){
        Toast.makeText(this, "Privacy Policy option selected", Toast.LENGTH_SHORT).show();
    }else if (item.getTitle()=="Terms of use"){
        Toast.makeText(this, "Terms of use option selected", Toast.LENGTH_SHORT).show();
    }
    return super.onOptionsItemSelected(item);
}

您的回答可以通过提供更多支持性信息来改进。请[编辑]以添加更多细节,例如引用或文档,以便其他人可以确认您的答案是正确的。您可以在帮助中心中找到有关如何撰写优秀答案的更多信息。 - Community

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