Android onCreateOptionsMenu is not displaying

戏子无情 提交于 2019-12-12 21:10:45

问题


In my android activity, i removed the Action Bar by using this Theme

 <style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>

but my problem is “onCreateOptionsMenu” is not display, I tried this method to create that pls see

 private void getOverflowMenu()
    {

        try
            {
                ViewConfiguration config = ViewConfiguration.get(Home.this);
                Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
                if (menuKeyField != null)
                    {
                        menuKeyField.setAccessible(true);
                        menuKeyField.setBoolean(config , false);
                    }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
    }

in this “onCreateOptionsMenu” is not displaying. please help me

but it displaying in Samsung

Because: in Samsung mobile has its own hardware menu button, but not in nexus etc

please see the image


回答1:


finally i got the answer

in my AndroidManifest.xml i just removed this line

   android:targetSdkVersion="18" 



回答2:


If you don't have action bar and neither a menu button (on most devices) where do you want to display your menu??

The easiest way is to use PopupMenu. Add a button somewhere in your layout and on the click even add the PopupMenu (Dropdown menu). If you want to use it for devices starting Android 2.1 use a Support Library.

You have HERE the android documentation about PopupMenu.

From SDK Manager you can download and see the support library v7 sample which includes PopupMenuHelper.

Hope it helps. Good Luck!



来源:https://stackoverflow.com/questions/23261125/android-oncreateoptionsmenu-is-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!