Libgdx align does not work

2019-08-21 02:33发布

After scaling down "Forgot Password:(" button, seems like its cell did not scale down. The result for that is that cell contents are in the bottom left of the cell. I need it to be aligned to the center or I need to scale down the cell to content's size.

Here is the screenshot:enter image description here

And the code:

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

What have I done wrong or missed?

1条回答
对你真心纯属浪费
2楼-- · 2019-08-21 02:36

Scaling an Actor does not adjust its preferred size. You have many ways to face this, choose the one you like the most.

  • scale the actor around its center. Call forgotButton.setOrigin(Align.center)
  • scale the font of the button, not the button. forgotButton.getLabel().setFontScale(.7f)
  • size the table's cell: table.add(forgotButton).pad(10).prefWidth(forgotButton.getPrefWith() * .7f)
查看更多
登录 后发表回答