gpt4 book ai didi

android - 我的简单 Android 应用程序不显示选项菜单

转载 作者:行者123 更新时间:2023-11-30 02:00:47 25 4
gpt4 key购买 nike

我正在尝试在 Android 中使用 AndEngine 开发一个简单的应用程序。我正在尝试添加选项菜单,但由于某些原因它没有显示出来。令人惊讶的是,选项菜单按钮甚至没有显示! (页面右下角的三点触摸按钮)。怎么了?

编辑:我已经有了 menu.xml。还是不行。

enter image description here

下面是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);
}
}


}

最佳答案

您需要在res/menu 文件夹中添加一个menu.xml。此文件将包含以下格式的列表的项目列表:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>

您还需要像这样从您的 Activity 中扩充菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

关于android - 我的简单 Android 应用程序不显示选项菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523111/

25 4 0