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

时间:2015-07-20 17:51:02

标签: android

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

编辑:我已经有menu.xml。它仍然无法运作。

enter image description here

以下是与MenuScene对应的代码段(AndEngine&#39的示例代码 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);
            }
        }


    }

1 个答案:

答案 0 :(得分:1)

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

<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>

您还需要从您的活动中为菜单充气,如下所示:

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