根据子角色调整表大小(LIBGDX)

时间:2019-01-29 12:16:08

标签: libgdx

我有一个主表(表),该表包含一个内表(表),该表包含一个Horizo​​ntalGroup,该表包含一个带有文本“ First words”的Label。

可维护   内部表     横向小组       标签1

在这种情况下,我的Maintable的宽度为195

enter image description here

然后添加带有“其他单词”的新标签。主表的大小仍然为195,您可以看到我的背景大小仍然相同。

enter image description here

如何实现像我的孩子一样根据子层次结构调整主表的大小?

代码:

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

}

1 个答案:

答案 0 :(得分:-1)

请勿通过删除maintable.setBounds调用来固定表格的大小。