Creating Options menu in Android

白昼怎懂夜的黑 提交于 2019-12-12 13:50:20

问题


I am trying to create options menu in my Android program. I am using the following code to inflate options menu :

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {     
     super.onCreateOptionsMenu(menu);   
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.optionsmenu, menu);
        return true;
   }

And my xml code is :

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

But with this code i am not able to show the options menu in my screen.

Also, i am using the code

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 

to make the activity as full screen view. Does this code creates problem in inflating the menu?

Thanks in advance,

Timson


回答1:


remove the line super.onCreateOptionsMenu(menu); from your onCreateOptionMenu. You are actually already providing the menu before inflating it.




回答2:


Options menu shows up by pressing the Options Menu button at the bottom of the phone




回答3:


Don't call

super.onCreateOptionsMenu(menu);

as that will return a value before your code is executed.




回答4:


Use this code:

public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.options_menu, menu);
      return true;
    }


来源:https://stackoverflow.com/questions/8970083/creating-options-menu-in-android

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