大家好,我正在尝试使用 Libgdx 中的表格显示一些图像,但它没有正确显示它们,我找不到解决该问题的方法
当我使用 table.bottom
时,它会显示它们向下,但是当我使用 table.center
时,它会显示它们较低,当我选择顶部、左侧或右侧时,它不会显示任何内容
:这是我用于格式化表格的代码:
public class MenuIcons {
public Viewport viewport;
public Stage stage;
public boolean upPressed;
public boolean leftPressed;
public boolean rightPressed;
public boolean levellock1,levellock2;
public Image lock1;
public Image lock2;
public OrthographicCamera camera;
public Table table;
//Constructor.
public MenuIcons(SpriteBatch spriteBatch) {
camera = new OrthographicCamera();
viewport = new FitViewport(400, 208, camera);
stage = new Stage(viewport, spriteBatch);
Gdx.input.setInputProcessor(stage);
initalizeTable();
}
public void draw() {
stage.draw();
}
public void resize(int width, int height) {
viewport.update(width, height);
}
public void initalizeTable()
{
//Buttons with images.
lock1 = new Image(new Texture("levellock.png"));
lock1.setSize(25, 25);
lock1.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
levellock1 = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
levellock1 = false;
}
});
lock2 = new Image(new Texture("levellock.png"));
lock2.setSize(25, 25);
lock2.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
levellock2 = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
levellock2 = false;
}
});
//Table with buttons.
table = new Table();
table.bottom; //Align to the left bottom.
table.add();
table.add();
table.add();
table.add(lock1).size(lock1.getWidth(), lock1.getHeight());
table.add(lock2).size(lock2.getWidth(), lock2.getHeight());
stage.addActor(table);
}
public boolean isLevellock1() {
return levellock1;
}
public boolean isLevellock2() {
return levellock2;
}
}
这是我使用Table.bottom
时的图像:
这是我使用Table.center
时的图像:
最佳答案
我建议阅读此处的信息和示例: https://github.com/libgdx/libgdx/wiki/Table
这条线甚至不起作用?:table.bottom;
试试这个 table.left().bottom();
而不是 table.bottom;
或者更好的是,您可以在添加单个项目时对齐它们,如下所示:
table.add(lock1).width(lock1.getWidth()).height(lock1.getHeight()).left().bottom();
注意:
为什么要一遍又一遍地使用空白的table.add();
?您不应该使用 table.row()
来代替吗?
编辑评论:
您需要为您的项目创建一个行或空间来适应,而不是使用table.add();
。你给自己带来了各种各样的问题。
在继续之前您需要在 table = new Table();
之后添加此 table.setDebug(true);
这样你就可以看到你在做什么。
现在试试这个:
table = new Table();
table.setDebug(true);
table.top().left();
table.add(lock1).width(lock1.getWidth()).height(lock1.getHeight()).left().top();
table.row();
table.row();
table.add(lock2).width(lock2.getWidth()).height(lock2.getHeight()).left().bottom();
现在您可以看到调试行,您可以看到哪里出了问题。在 table.row();
之间添加一个空白标签并使用 .expand();
以便它填充空间,如下所示:
table.row();
table.add(myBlankLabel).expandY();
table.row();
关于java - Libgdx 表组件未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37737474/