Michael Kleinhenz
Michael Kleinhenz

Reputation: 473

How to vertically align actors inside a libGDX table cell?

I'd like to create a table in libGDX with centered actors inside cells. My current code:

Table table = new Table();
table.left().top();
table.setDebug(true);

Image drawable = new Image(TextureCache.getTexture(imageName));
table.add(drawable).pad(10);

label = new Label(text);
label.setWrap(true);
table.add(label).pad(10);

..creates a table with the image vertically centered (the label text is multiline text that blows up the row height). How do I align the cells so that actors in cells are aligned at top?

Upvotes: 2

Views: 6014

Answers (1)

Tenfour04
Tenfour04

Reputation: 93779

When you add the cell, tell it to fill Y so the cell itself will be the same height of the row. Then you can align its contents. If you don't do this and you don't specifically set its width and height, then the cell is only the size of the object in it so alignment doesn't do anything (turn on table.debug() to see this).

Image drawable = new Image(TextureCache.getTexture(imageName));
table.add(drawable).pad(10).fillY().align(Align.top);

By the way, using static references to Textures in libgdx will lead to memory leaks and graphical bugs if you target Android because there is no way to ensure that the textures are disposed of when the Activity closes.

Upvotes: 5

Related Questions