1

I made .pack file and load it to skin. I can set it as drawable for Image class and it works fine. But Table.setBackground do nothing.

Drawable background = skin.getDrawable("tooltip_background");
tooltipGroup.setBackground(background);

Why this code not work?

Full code:

tooltipGroup = new Table(skin);
                tooltipGroup.setWidth(w/6);
                Label.LabelStyle headerStyle = new Label.LabelStyle(headerFont, Color.GREEN);
                Label headerLabel = new Label(effect.getName(), headerStyle);
                headerLabel.setWidth(w/6);
                headerLabel.setX(20);
                headerLabel.setWrap(true);
                headerLabel.setAlignment(Align.topLeft);
                tooltipGroup.addActor(headerLabel);
                Label.LabelStyle style = new Label.LabelStyle(font, Color.WHITE);
                Label descriptionLabel = new Label(effect.getDescription(), style);
                descriptionLabel.setWidth(w/6);
                descriptionLabel.setWrap(true);
                descriptionLabel.setAlignment(Align.topLeft);
                descriptionLabel.setPosition(20, -descriptionLabel.getHeight());
                tooltipGroup.addActor(descriptionLabel);
                tooltipGroup.setPosition(mouseX, h - mouseY);
                Drawable background = skin.getDrawable("tooltip_background");
                tooltipGroup.setBackground(background);
                stage.addActor(tooltipGroup);
V. Govorov
  • 109
  • 10
  • I found what's wrong. Table has width and height = 0 on init. And... Adding items with their own sizes to Table not changing width and height of Table. Why? And how can I work with that, if Label with `setWrap(true)` returns height only of one row? – V. Govorov Jun 23 '17 at 06:34
  • Paste the full code for constructing the table, and adding it to the scene. Actors added without a minimum width / height, don't automatically fill the scene, so either set a min width / height, or set them to fill the entire scene. – Bernd Jun 23 '17 at 06:53
  • @Bernd check question for full code. And... If I set minimal width / height, than table will use it? Ok. But real width and height of content signifently more than theoreticly choosed minimals. – V. Govorov Jun 23 '17 at 07:05

1 Answers1

0

Don't use addActor on the Table. In order to add Actors to the table, use the add method, and specify custom parameters like fill or width / height of the actors in chaining methods. Like this, you can control how you add the actors to the table, since the table does auto layouting.

Table rootTable = new Table();
rootTable.setFillParent(true);

TextField fieldFirst = new TextField("First", skin);
TextField fieldSecond = new TextField("Second", skin);

rootTable.add("First:");
rootTable.add(fieldFirst).width(100).row();
rootTable.add("Second:");
rootTable.add(fieldSecond).width(100);

stage.addActor(rootTable);

As you can see, this allows you to specify the width, in the table, with the expand(), and fill() methods, you can even have an element fill all the available space within one row. Once you use this auto layouting of your table, the table should have the appropriate size, and your background should show. More on how to use the libGdx table layouting can be found here.

Bernd
  • 779
  • 5
  • 11
  • What can I do, if i don't know height of Label? Wrapped label can't return it's real height. It's returns height of one row of text. – V. Govorov Jun 23 '17 at 07:41
  • You don't need to specify the height of the label, the table should automatically layout everything for you. If the wrapped label is taller than all other actors, the row will simply have the height of the wrapped label. Take a look at this answer for more info: https://stackoverflow.com/a/18557487/4735988 – Bernd Jun 23 '17 at 07:50