我正在使用表格为舞台添加一些标签.出于某种原因,它将其添加到右下角而不是左上角.关于如何正确布置组件的任何想法?
int dialogWidth = 450; int dialogHeight = 650; Label label1 = new Label("MUSIC:", skin); Label label2 = new Label("SOUND EFFECTS:", skin); Label label3 = new Label("JUMP SPEED:", skin); TextButton btn1 = new TextButton("whatever", skin); final Dialog dialog = new Dialog("Properties", skin); dialog.setSize(dialogWidth, dialogHeight); dialog.setX(Helper.RESOLUTION_X_CENTER-dialogWidth/2); dialog.setY(Helper.RESOLUTION_Y_CENTER-dialogHeight/2); Table t = new Table(); t.setBounds(0, 0, dialogWidth, dialogHeight); t.add(label1); t.row(); t.add(label2); t.add(btn1); t.row(); t.add(label3); dialog.add(t); stage.addActor(dialog);
解决方案:删除以下行
t.setBounds(0, 0, dialogWidth, dialogHeight);
原因:不要自己使用setBounds.让layoutmanager处理它.
解决方案:替换以下行
dialog.add(t);
同
dialog.getContentTable().add(t).expand().left().top();
原因: Dialog本身就是一个包含3个组件的表,即Title,ContentTable,ButtonTable.
你的代码是在ButtonTable(因此在右边)之后的ButtonTable行中添加新表(最后一行因此在底部).
更新后的代码将其添加到应该是的内容表中.
请参阅https://github.com/EsotericSoftware/tablelayout以了解背后的原因
expand().left().top();
最后,要调试任何与Table相关的问题,请考虑使用调试行(在同一链接中解释)
希望这会有所帮助.