Android Options Menu not displaying
I am making a game for Android and the simplest implementation of an Options Menu doesn't work - the game itself is in a seperate class (Engine.java), that extends a LinearLayout, so I am wondering if that is why it is not visible - however the other "top-level" @Overrides like onStop(), onPause(), etc. all fire just fine - is there something I have to do inside my game class to get it to display an options menu?
The code base is from Pro Android Games SpaceBlaster example: http://www.apress.com/9781430226475 (source can be downloaded)
MyGame.class (entrance point of application):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater factory = LayoutInflater.from(this);
// Set game layout
view = factory.inflate(R.layout.main, null);
setContentView(view);
// Enable view key events
view.setFocusable(true);
view.setFocusableInTouchMode(true);
} //End OnCreate()
@Override
开发者_如何学JAVA public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
res\menu\menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon"
android:icon="@drawable/icon" />
<item android:id="@+id/text"
android:title="Text" />
<item android:id="@+id/icontext"
android:title="Icon and text"
android:icon="@drawable/icon" />
</menu>
layout\main.xml:
<game.thegame.test.Engine
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_absolute"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000">
</game.thegame.test.Engine>
Make sure you have a title in all your menu items!
Items without a title, I guess, can be displayed in the action bar but not as a list when pressing the menu button. Below is a simple example..
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_menu" >
<item android:id="@+id/action_undo"
android:icon="@drawable/ic_action_undo"
android:title="@string/mb_undo"
android:showAsAction="always"
android:onClick="onPressUndo" />
As far as I can see, if everything is fine with the code and menu resource files, the reason is not adding additional styles resource directory for a low level API.
Here is my set;
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.levent.learning.notetaker">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
A) MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
B) Resource File For Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/hede"
android:title="@string/hede_item"></item>
</menu>
C) Resource File : styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
Everything seems fine with these right ? But even with these, Options menu is not visible. So I've created and additional values resource directory for API Level 11, named values-v11, and added a additional styles.xml file for that.
SOLUTION 1
D) Resource file for API Level 11
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
</style>
</resources>
Then it works !
Here is my working configuration, to make two distinct folders for the values resource, I've selected the Project
option in Android;
SOLUTION 2
Remove the following theme definition line from AndroidManifest.xml file;
android:theme="@style/AppTheme"
So that the new AndroidManifest.xml is as follows;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.levent.learning.notetaker">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm a newbie on android development, using Android Studio version 2.1.2. Even this question is seems so old, it is still not obsolete. I hope this answer helps someone out there, cause this problem drove me crazy until I've found the empirical solution, hope that it helps. Happy coding!
Directly we can add menu items like this , it working fine for me
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuItem item1=menu.add(0, 4, 0,"text1");
item1.setIcon(R.drawable.car);
MenuItem item2=menu.add(0, 0, 0, "text2");
item2.setIcon(R.drawable.share);
MenuItem item3=menu.add(0, 2, 0, "text3");
item3.setIcon(R.drawable.history);
MenuItem item4=menu.add(0, 3, 0, "text4");
item4.setIcon(R.drawable.settings);
return true;
}
精彩评论