Scene2D UI 没有填满整个屏幕

Scene2D UI not filling whole screen

在我目前正在处理的关卡选择屏幕中,我似乎无法让 Scene2D UI table 填满整个屏幕。 我从代码中省略了一些位以减少长度,但这里是相关代码:

Stage stage = new Stage(new ScreenViewport());

table = new Table();
table.setFillParent(true);
innerTable = new Table();
//Omitted: Add a button to innerTable for each level

ScrollPaneStyle scrollPanelStyle = new ScrollPaneStyle();
scrollPanelStyle.vScrollKnob = new BaseDrawable();
scrollPane = new ScrollPane(innerTable);
scrollPane.setScrollingDisabled(true, false);
scrollPane.setupOverscroll(30f, 30f, 150f);

//Temporarily a restart icon 
Image backButton = new Image(backButtonTexture);
backButton.setScaling(Scaling.fit);
table.add(backButton).height(75f).width(75f).pad(20, 0, 20, 0).left();
table.row();
table.add(scrollPane);

目前看起来是这样的。该图标并没有像我认为的当前代码那样靠左。就我而言,它应该在 X 轴上为 0。

对于包含所有级别的 ScrollPane 的 innerTable 也是如此,它没有在 X 轴上填满整个屏幕,但看起来我想要它,所以这不是真正的问题。

答案:expandX() 添加到我的 ScrollPane 最终成功了,结果是这一行:table.add(scrollPane).expandX();。非常感谢 flogy。

我想 table 已正确拉伸到您的视口,但还有其他一些错误配置。我建议使用调试模式来获取更多详细信息(请参阅 https://github.com/libgdx/libgdx/wiki/Table#debugging - 也可用于后退按钮等)。如果您无法自行使用此方法找到解决方案,请更新屏幕截图,以便我们进一步调查。谢谢

最终解决方案(见评论):

 table.add(scrollPane).expandX();

table 已经填满了整个屏幕,但包含的滚动面板却没有。 table 单元格默认适应它们的内容。通过使用 expandX,我们最终可以将包含滚动面板的单元格的宽度扩展到完整的 table 宽度。