将文本按钮居中到屏幕libgdx

Ric*_*ton 2 java android libgdx

我目前正在使用LibGDX创建游戏,目前我正在创建我的标题屏幕.我无法在屏幕上居中显示TextButtons,如果将TextButton的宽度设置为舞台的宽度,我可以让它们居中,但是如果你点击该按钮从左到右的任何地方都可以按下按钮而不是你只需点击按钮本身.

我如何让TextButtons以游戏屏幕为中心并以彼此为中心?我有三个TextButtons(新游戏,继续和退出游戏).

任何帮助将不胜感激,谢谢!

编辑:

这是我的三个按钮的代码.

btnNG = new TextButton("New Game", btnStyle[0]) {
    {
        setName("New Game");
        addListener(new InputListener() {
            public boolean touchDown(InputEvent e, float x, float y, int pointer, int button) {
                g.debugOut(btnNG.getName(), "touchDown?");
                return true;
            }

            public void touchUp(InputEvent e, float x, float y, int pointer, int button) {
                g.debugOut(btnNG.getName(), "touchUp?");
                //g.switchScreen(new Mainscreen(g));
            }
        });
        setWidth(sTitle.getWidth());
        setPosition(0, (getHeight() * 2.5f));
    }
};

btnContinue = new TextButton("Continue", btnStyle[0]) {
    {
        setName("Continue");
        addListener(new InputListener() {
            public boolean touchDown(InputEvent e, float x, float y, int pointer, int button) {
                g.debugOut(btnContinue.getName(), "touchDown?");
                return true;
            }

            public void touchUp(InputEvent e, float x, float y, int pointer, int button) {
                g.debugOut(btnContinue.getName(), "touchUp?");
            }
        });
        setWidth(sTitle.getWidth());
        setPosition(0, (getHeight() * 1.4f));
    }
};

btnExit = new TextButton("Exit Game", btnStyle[0]) {
    {
        setName("Exit Game");
        addListener(new InputListener() {
            public boolean touchDown(InputEvent e, float x, float y, int pointer, int button) {
                g.debugOut(btnExit.getName(), "touchDown?");
                return true;
            }

            public void touchUp(InputEvent e, float x, float y, int pointer, int button) {
                g.debugOut(btnExit.getName(), "touchUp?");
                //Gdx.app.exit();
            }
        });
        setWidth(sTitle.getWidth());
        setPosition(0, (getHeight() * .3f));
    }
};
Run Code Online (Sandbox Code Playgroud)

这就是我到目前为止,它是居中的,但这样做可以通过按屏幕上任意位置到实际文本的左/右来点击它.

Tan*_*til 5

我猜sTitle是你的舞台对象......


你是否直接在舞台上添加按钮?

如果是这样,我建议使用表格.

Table menuTable = new Table();
menuTable.add(btnNG);
menuTable.row();
menuTable.add(btnContinue);
menuTable.row();
menuTable.add(btnExit);
menuTable.setFillParent(true);
sTitle.addActor(menuTable);
Run Code Online (Sandbox Code Playgroud)

注1:
删除所有按钮上的所有setSize和setPosition调用.
删除其他addActor调用.

注意2:
有关高级功能,请参阅TableLayout