【问题标题】:LibGDX - How to increase a cell's size without affecting its content?LibGDX - 如何在不影响其内容的情况下增加单元格的大小?
【发布时间】:2018-05-13 17:16:13
【问题描述】:

我正在尝试使用 LibGDX 的 Scene2d,并编写了以下简单代码:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;

public class UITest extends ApplicationAdapter {

    private Stage stage;

    public static void main(String[] args) {
        LwjglApplicationConfiguration configuration = new LwjglApplicationConfiguration();
        configuration.title = "UI Test";
        configuration.width = 800;
        configuration.height = 600;
        new LwjglApplication(new UITest(), configuration);
    }

    @Override
    public void create() {
        this.stage = new Stage();

        Skin skin = new Skin(Gdx.files.internal("skins/default.json"));

        Table table = new Table();
        table.setFillParent(true);
        table.add(new Label("Text", skin));

        table.debugAll();

        this.stage.addActor(table);
    }

    @Override
    public void render() {
        this.stage.draw();
    }
}

运行时会产生以下结果:

现在我想将单元格的内容对齐到左上角。为此,我将单元格的宽度和高度设置为 100 并应用对齐方式,如下所示:

table.add(new Label("Text", skin)).width(100).height(100).top().left();

这会导致单元格的内容与单元格一起增加:

这是预期的行为吗?如果是这样,有没有办法在不影响其内容的情况下增加单元格的大小?

使用单元格的expand 方法不会影响其内容,但是我希望更精确地控制单元格的大小,将其设置为特定值或百分比。

【问题讨论】:

  • 单元格内容增加是什么意思,在上面的屏幕截图中单元格大小增加但内容(标签)大小相同。
  • @Aryan 没错,标签最终与单元格大小相同。我想在不增加标签的情况下增加单元格的大小。

标签: libgdx scene2d


【解决方案1】:

这是意料之中的。当您在单元格上设置显式大小时,相同的大小将应用于单元格中的演员。

要获得您想要的行为,您应该将表格本身设置为您想要的整体大小。然后在单元格上使用expand() 使其填满表格。由于您没有在单元格上设置明确的大小,因此参与者仍然可以控制自己的大小,并且您可以在其单元格上使用对齐来使其在单元格中对齐。

或者,您也可以直接在标签上设置对齐方式,即使标签 Actor 正在填充单元格,它也会对齐自身内部的文本。

【讨论】:

  • 感谢您的回答!我找到了另一种实现我想要的更适合我的情况的方法:将Label 放在Container 中。这样Container 的大小与单元格相同,但Label 保持其原始大小。无论如何,我会接受你的回答,因为我没有指定我的场景。
猜你喜欢
  • 2012-01-20
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 2013-02-14
  • 2016-04-18
  • 1970-01-01
相关资源
最近更新 更多