如何显示在安卓4.2选项菜单 [英] How to show option menu in android 4.2

查看:184
本文介绍了如何显示在安卓4.2选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建在我的测试应用程序的菜单选项。

I am trying to create menu option in my test application.

我能看到菜单,当我在清单设定主题,只是默认值(菜单显示在顶部)。如果我在清单中设置主题,以NoTitleBar。我看不到菜单选项?

I am able to see the menu when I set the theme in manifest to just default (The menu shows up in the top). If I set the theme in manifest to NoTitleBar. I can't see the menu option?

我想要得到的菜单时,我在清单中设置主题为NoTitleBar。

I want to get the menu when I set theme "NoTitleBar" in manifest.

如何解决?

以下是我已经用我的测试应用程序的事情:

Below are things that I have used in my test application:

与清单:

  <uses-sdk
      android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >
    <activity
        android:name="com.ssn.menuoptions.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

menu.xml文件

Menu.xml

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/menu_preferences"
      android:title="Preferences" /> 
 </menu>

Java的文件:

Java file:

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

是否有可能得到这样的:

Is it possible to get something like this:

如何使菜单下出现?

谢谢!

推荐答案

添加按照以下Activity.setContentView code();

Add following code below Activity.setContentView();

 setContentView(R.layout.activity_detail);
    // set option menu if has no hardware menu key
    boolean hasMenu = ViewConfiguration.get(this).hasPermanentMenuKey();
    if(!hasMenu){
        //getWindow().setFlags(0x08000000, 0x08000000);
        try {
            getWindow().addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
          }
          catch (NoSuchFieldException e) {
            // Ignore since this field won't exist in most versions of Android
          }
          catch (IllegalAccessException e) {
            Log.w("Optionmenus", "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
          }
    }

这篇关于如何显示在安卓4.2选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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