我一直在使用 libgdx 开发一些应用程序,但我不知道如何通过坐标定位按钮。
public void create() {
batch = game.getSpriteBatch();
background = new Texture("data/textures/menuScreen_small.png");
bubbleArray = new Array<Bubble>();
Gdx.input.setInputProcessor(stage);
for (int i = 0; i < 20; i++) {
bubbleArray.add(new Bubble(MathUtils.random(0,
Gdx.graphics.getWidth()), MathUtils.random(0,
Gdx.graphics.getHeight())));
}
table = new Table();
stage.addActor(table);
table.setSize(1280, 720);
table.setPosition(0, 0);
//table.left().top();
// table.debug();
//table.row().minHeight(55).maxHeight(55);
skin = new Skin(Gdx.files.internal("data/ui/uiskin.json"));
/*
* HomeScreen [Button 1]
*/
final TextButton camButton = new TextButton("Camera", skin);
//Add button
table.add(camButton);//.left().expandX().fillX();
camButton.setPosition(0, 0);
//Button Height
//table.row().minHeight(55).maxHeight(55);
//Button Width
//table.row().minWidth(55).maxWidth(55);
在上面的代码中,我试图将我的文本按钮“camButton”定位在屏幕上的 0,0 处。
当我启动程序时,它位于屏幕的正中央。
如果有人有一些指南或技巧让我知道,我需要学习这些东西。
一个小的“修复”
不完全是修复;但通过删除表一般解决了这个问题。通过这样做,我将它添加到舞台而不是舞台 > 表格 > 按钮。对此有何评论?
最佳答案
如果您只想将按钮固定到绝对位置,例如 0,0,则无需将项目添加到表格中,只需将按钮添加到舞台上即可
TextButton button = new TextButton("Floating Button", skin);
button.setPosition(10, 10);
stage.addActor(button);
编辑 如果您仍然想要表格提供的受控定位,请查看表格布局指南,其中有很多很好的示例 Table Layout
如果要定位表格
table.left().bottom();
在表格中定位项目
table.add(button).expand().bottom().right();
关于java - 定位按钮绝对 x,y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25189046/