简体   繁体   中英

Libgdx align does not work

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: 在此处输入图片说明

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM