如何在 Table 上设置背景颜色或图像?

How to set background color or image on a Table?

我正在制作一个 RTS 游戏,我想要一个类似于 UI 的帝国时代,底部有 UI。 我很难设置包含特定元素的 Table 的背景。

我尝试了很多东西,比如:

但似乎没有任何效果。我认为问题在于我尝试将背景颜色应用于包含其他 table 的最后面的 table。我可以为正面 table 设置颜色,但不能为主要的背面设置颜色。

这里是我画的地方 UI:

        skin = new Skin(Gdx.files.internal("core/assets/skinComposerExport/uiskin.json"));
        stage =         new Stage(new ScreenViewport());
        mainTable =     new Table(skin);
        unitInfoTable = new Table(skin);
        unitSpecTable = new Table(skin);

        className =     new Label("Placeholder scnm", skin);
        ...
        abilityCD =     new Label("Placeholder acdn", skin);

        mainTable.setPosition(Gdx.graphics.getWidth() / 2.0f, 0);
        mainTable.center().bottom();
        mainTable.setBackground("UI_background");


        addLabelsToTable(unitInfoTable, className, hitPoints, mana, dmg, range);

        addLabelsToTable(unitSpecTable, abilityName, abilityMana, abilityDmg, abilityRange, abilityCD);

        mainTable.add(unitInfoTable).padRight(20);
        mainTable.add(unitSpecTable);


        stage.addActor(mainTable);

我想要整个屏幕底部都是白色的(大约200px高并填满整个x轴)并在上面绘制元素,但是我不能设置背景颜色。

您可以为 table 指定一个尺寸,以便您的舞台知道如何正确渲染它。 (或者在你的外部 table 上使用 setFillParent(true))。通常,我创建一个 root table 作为舞台的大小,然后我向该 root table 添加额外的 scene2d 元素,并根据需要对齐。

这是一个使用 ShadeUI

的简单示例
@Override
public void create() {
    stage = new Stage(new ScreenViewport());
    batch = new SpriteBatch();
    skin = new Skin(Gdx.files.internal("shadeui/uiskin.json"));

    // create a root table, sized to our stage, and add it to the stage
    Table root = new Table();
    root.setBackground(skin.getDrawable("dialogRed"));
    root.setSize(stage.getWidth(), stage.getHeight());
    stage.addActor(root);

    // now create our menu, bottom-aligned, filled to width, and add it to root
    Table menu = new Table();
    menu.setBackground(skin.getDrawable("dialogDim"));
    root.add(menu).expand().bottom().fillX().height(50);

    // add additional labels to the menu
    menu.defaults().expandX().center().uniformX().uniformX();
    menu.add(new Label("HP", skin));
    menu.add(new Label("MP", skin));
    menu.add(new Label("My Name", skin));
    menu.add(new Label("My Class", skin));
}

@Override
public void render() {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    stage.draw();
    batch.end();
}

这是我 运行 时的样子: