Libgdx对齐不起作用

时间:2018-05-30 08:51:12

标签: java layout libgdx

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

以下是屏幕截图:enter image description here

代码:

 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();

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

1 个答案:

答案 0 :(得分:1)

缩放Actor不会调整其首选大小。你有很多方法可以解决这个问题,选择你最喜欢的方式。

  • 将演员围绕其中心进行缩放。致电forgotButton.setOrigin(Align.center)
  • 缩放按钮的字体,而不是按钮。 forgotButton.getLabel().setFontScale(.7f)
  • 调整表格的大小:table.add(forgotButton).pad(10).prefWidth(forgotButton.getPrefWith() * .7f)
相关问题