在 Android Studio 中实现选项菜单 [英] Implementing an option menu in Android Studio

查看:58
本文介绍了在 Android Studio 中实现选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的 android 应用程序中实现选项菜单?我尝试了来自

解决方案

在你的java代码中,添加这个onCreateOptionsMenu来显示optionMenu,

@Override公共布尔 onCreateOptionsMenu(菜单菜单){MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.option_menu, menu);//你的文件名返回 super.onCreateOptionsMenu(menu);}

保留您的 res\menu\option_menu 文件夹下,

</菜单>

现在,如果您想设置 onOptionsItemSelected 即您可以使用的 onClick 事件,

@Override公共布尔 onOptionsItemSelected(最终菜单项){开关(item.getItemId()){案例android.R.id.new_game://你的代码//EX : 如果你想切换到其他活动,调用意图返回真;案例R.id.帮助://你的代码返回真;默认:返回 super.onOptionsItemSelected(item);}}

How do I implement an option menu in my android application? I tried code from Android Developer but I get errors. Such as these: Element menu must be declared. Here is my code

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

解决方案

In your java code, add this onCreateOptionsMenu to show optionMenu,

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

Keep your under res\menu\option_menu folder,

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

Now, if you want to set onOptionsItemSelected i.e onClick event for that ou can use,

@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);
        }
    }

这篇关于在 Android Studio 中实现选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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