表格布局不表现为预期
问题描述:
我有这样的代码建立了一块UI的:表格布局不表现为预期
// Trying to fix the layout by setting the defaults for the next row.
layout.row().expand(false, false).fill(false).colspan(4);
Label playerLabel = new Label(Settings.playerName + " " + playerScore, skin, "defaultWhite");
layout.add(playerLabel).colspan(1);
Label historyLabel = new Label(getHistory(), skin, "defaultWhite");
historyLabel.setAlignment(2);
layout.add(historyLabel).colspan(2).expandX();
Label cpuLabel = new Label(cpuScore + " CPU", skin, "defaultWhite");
layout.add(cpuLabel).colspan(1);
layout.row();
我预计它使玩家和CPU分数被推到侧面和历史标签来获取剩余空间,但是这是它在做什么:
难道我做错了什么吗?我看不出有什么可能是错误的。
如果您想查看其余的代码,请查看github,或者只是询问,我会提供更多的上下文。
答
而不是使用一个表执行整个布局,请尝试嵌套表,其中每个内部表是一行。这消除了对所有colspan的需求,您可以创建列并根据需要布置每个行表,而不会影响其他行。
答
如果你不希望使用更多的表在接受答案的建议,我设法得到它通过调用得分细胞uniform()工作,像这样:
layout.row().expand(false, false).fill(false).colspan(4);
Label playerLabel = new Label(Settings.playerName + " " + playerScore, skin, "defaultWhite");
layout.add(playerLabel).colspan(1).uniform();
Label historyLabel = new Label(getHistory(), skin, "defaultWhite");
historyLabel.setAlignment(2);
layout.add(historyLabel).colspan(2).expandX();
Label cpuLabel = new Label(cpuScore + " CPU", skin, "defaultWhite");
layout.add(cpuLabel).colspan(1).uniform();
layout.row();
这就是我要与此同时,我还通过在分数标签上使用uniform()方法,让它像我想要的那样工作,但使用更多的表格可以让我更好地控制布局。 – TheOsirian