Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在android 4.2中显示选项菜单大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的测试应用程序中创建菜单选项.

当我将清单中的主题设置为认时,我能够看到菜单(菜单显示在顶部).如果我将清单中的主题设置为NotitleBar.我看不到菜单选项?

我想在清单中设置主题“NotitleBar”时获取菜单.

怎么解决

以下是我在测试应用程序中使用的内容

与清单:

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

@H_32_2@menu.xml文件

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

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.main,menu);
    return true;
}

有可能得到这样的东西:

如何让菜单显示下来?

谢谢!

解决方法

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

大佬总结

以上是大佬教程为你收集整理的如何在android 4.2中显示选项菜单全部内容,希望文章能够帮你解决如何在android 4.2中显示选项菜单所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。