Libgdx 对齐不起作用

Libgdx align does not work

缩小 "Forgot Password:(" 按钮后,它的单元格似乎没有缩小。结果是单元格内容位于单元格的左下角。我需要它与中心对齐,或者我需要将单元格缩小到内容的大小。

截图如下:

代码:

 Table tableRoot = new Table();
 tableRoot.setFillParent(true);
 tableRoot.padTop(40);
 tableRoot.align(Align.center);

 final Label hint = new Label("Wrong password", Utility.UI_SKIN, "default-text");
 hint.setAlignment(Align.center);
 hint.setFontScale(1.3f);

 final Label question = new Label("Return to login screen?", Utility.UI_SKIN, "default-text");
 question.setAlignment(Align.center);

 Table table = new Table();
 final TextButton yesButton = new TextButton("Yes", Utility.UI_SKIN, "login-window-button");
 final TextButton forgotButton = new TextButton("Forgot password:(", Utility.UI_SKIN, "login-window-button");
 forgotButton.setTransform(true);
 forgotButton.setScale(0.7f);

 table.add(yesButton).pad(10).row();
 table.add(forgotButton).pad(10).align(Align.center); //align here does not work

 tableRoot.add(hint).growX().row();
 tableRoot.add().grow().row();
 tableRoot.add(question).growX().padBottom(20).row();
 tableRoot.add(table).growX().row();
 tableRoot.add().grow().row();
 tableRoot.add().grow().row();
 tableRoot.pack();

我做错了什么或错过了什么?

缩放 Actor 不会调整其首选大小。你有很多种方式来面对这个,选择你最喜欢的一种。

  • 围绕其中心缩放 actor。致电 forgotButton.setOrigin(Align.center)
  • 缩放按钮的字体,而不是按钮。 forgotButton.getLabel().setFontScale(.7f)
  • 调整 table 的单元格大小:table.add(forgotButton).pad(10).prefWidth(forgotButton.getPrefWith() * .7f)