简体   繁体   English

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

[英]How to set background color or image on a Table?

I'm making an RTS game and I want to have a similar UI to Ages of Empires with the UI at the bottom. 我正在制作RTS游戏,并且我想要一个类似于《帝国时代》的UI,其UI位于底部。 I have difficulties setting the background of the Table that holds the specific elements. 我很难设置包含特定元素的表格的背景。

I tried a lot of things, like: 我尝试了很多事情,例如:

but nothing seems to work. 但似乎没有任何效果。 I think the problem is that I try to apply the background color to the back-most table that holds other tables. 我认为问题是我尝试将背景色应用于包含其他表格的最背面表格。 I can set color for the front tables, but not for the main, back one. 我可以为前面的桌子设置颜色,但不能为主要的后面的桌子设置颜色。

Here is where I draw the UI: 这是绘制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);

I want the whole bottom of the screen to be white (about 200px tall and fill the whole x axis) and draw elements on it, but I can not set the background color. 我希望屏幕的整个底部为白色(约200像素高,并填充整个x轴)并在其上绘制元素,但是我无法设置背景色。

You can give your table a size so your stage knows how to render it correctly. 您可以给表指定大小,以便您的舞台知道如何正确呈现它。 (Or use setFillParent(true) on your outer table). (或在外部表上使用setFillParent(true))。 Typically, I create a root table that is the stage's size, then I add additional scene2d elements to that root table, aligning as needed. 通常,我创建一个舞台大小的root表,然后向该root表添加其他scene2d元素,并根据需要进行对齐。

Here is a simple example using ShadeUI 这是一个使用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();
}

And here is how it looks when I run it: 这是我运行它时的外观: UI图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM