我試圖在Android中使用AndEngine開發一個簡單的應用程序。我想添加選項菜單,但由於某些原因,它不顯示。令人驚訝的是,選項菜單按鈕甚至沒有顯示! (頁面右下方的3點觸摸按鈕)。怎麼了?我的簡單Android應用程序不顯示選項菜單
編輯:我已經有menu.xml
。它仍然不起作用。
這裏是對應於MenuScene
(AndEngine的示例代碼here)
public class MainActivity extends SimpleBaseGameActivity implements IOnMenuItemClickListener {
private static int CAMERA_WIDTH ;
private static int CAMERA_HEIGHT;
protected static final int MENU_ADD = 0;
protected static final int MENU_QUIT = MENU_ADD + 1;
private Font mFont,menuFont;
private MenuScene mMenuScene;
Camera camera;
Scene scene;
@Override
public EngineOptions onCreateEngineOptions() {
//default code
}
@Override
public void onCreateResources() throws IOException {
//some code
}
@Override
public Scene onCreateScene() {
//some code
}
@Override
public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
switch(pMenuItem.getID()) {
case MENU_ADD:
/* Restart the animation. */
Log.i("hello", "Menu ADD CLICKED");
return true;
case MENU_QUIT:
/* End Activity. */
this.finish();
return true;
default:
return false;
}
}
protected MenuScene createMenuScene() {
final MenuScene menuScene = new MenuScene(this.camera, new AlphaMenuSceneAnimator());
final IMenuItem resetMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_ADD, this.menuFont, "ADD ITEM", this.getVertexBufferObjectManager()), new Color(1,0,0), new Color(0,0,0));
menuScene.addMenuItem(resetMenuItem);
final IMenuItem quitMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, this.menuFont, "QUIT", this.getVertexBufferObjectManager()), new Color(1,0,0), new Color(0,0,0));
menuScene.addMenuItem(quitMenuItem);
menuScene.buildAnimations();
menuScene.setBackgroundEnabled(false);
menuScene.setOnMenuItemClickListener(this);
return menuScene;
}
@Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
if(this.scene.hasChildScene()) {
/* Remove the menu and reset it. */
this.mMenuScene.back();
} else {
/* Attach the menu. */
this.scene.setChildScene(this.mMenuScene, false, true, true);
}
return true;
} else {
return super.onKeyDown(pKeyCode, pEvent);
}
}
}
添加的代碼在'菜單。 xml'文件。它在你的項目的'res'文件夾中。 –
@SamratDutta那麼,最初的AndEngine示例項目甚至沒有menu.xml文件,但它在那裏工作!我有我的項目。我應該輸入哪些代碼? –
是的,我查過了。您不需要menu.xml中的代碼,因爲菜單項是動態生成的。我正在研究它。 –