我有一个maintable(Table),它包含一个innertable(Table),它包含一个HorizontalGroup,其中包含一个带有文本“firstwords”的标签。
主表
内表
水平群
标签1
在这种情况下,主表的宽度是195
然后添加一个带有“附加单词”的新标签。主表仍然是195号,您可以看到我的背景仍然是相同的大小。
我怎样才能实现,主表的大小取决于子层次结构,就像我的一样?
代码:
package com.xxx.tests.dialogtest.tabletest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.xxx.layouts.SkinManager;
public class TableTestMitInnerTable extends ApplicationAdapter {
private Stage stage;
private Table maintable;
private Table innerTable;
private HorizontalGroup horizontalGroup;
private Skin skin;
private int counter = 0;
public void create() {
stage = new Stage(new ScreenViewport());
skin = new Skin(Gdx.files.internal(SkinManager.getSkin()));
maintable = new Table();
maintable.setDebug(true);
innerTable = new Table();
//innerTable.setDebug(true);
horizontalGroup = new HorizontalGroup();
Label label = new Label("First words.", skin);
horizontalGroup.addActor(label);
innerTable.add(horizontalGroup);
// horizontalGroup.setFillParent(true);
// innerTable.setFillParent(true);
maintable.add(innerTable).pad(20);
stage.addActor(maintable);
maintable.setPosition(300,300);
maintable.setBounds(maintable.getX(), maintable.getY(), maintable.getPrefWidth(), maintable.getPrefHeight());
maintable.setBackground(new NinePatchDrawable(getNinePatch(("skins/gacstyle/raw/window.9.png"))));
System.out.println("Size before adding label : " + maintable.getWidth());
Gdx.input.setInputProcessor(stage);
}
private NinePatch getNinePatch(String fname) {
// Get the image
final Texture t = new Texture(Gdx.files.internal(fname));
return new NinePatch( new TextureRegion(t, 1, 1 , t.getWidth() - 2, t.getHeight() - 2), 10, 10, 10, 10);
}
@Override
public void render() {
Gdx.gl.glClearColor(0.2f, 0.3f, 0.5f, 0.6f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
counter++;
if(counter == 1){
horizontalGroup.addActor(new Label(" additional words ! ", skin));
System.out.println("Size after adding label : " + maintable.getWidth());
}
super.render();
stage.draw();
stage.act();
}
}