简体   繁体   中英

Libgdx table setBackground does nothing

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

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 .

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