具有多个小部件的 LibGDX 和 ScrollPane

2022-01-12 00:00:00 android java libgdx

尝试将多个项目添加到滚动窗格中,我很快发现所有addActor"函数都不支持.因此,我添加了一个包含我想要的所有项目的表格(此代码错过了我仍想添加的图像)以制作可滚动的信用屏幕......但这种方法(当前)不允许溢出,渲染ScrollPane 没用.(我的文字只显示到屏幕允许的高度,并且不可滚动).在 LibGDX 中制作具有多个小部件的可滚动窗格的方法是什么?(我目前只关心Android和Win/Lin/Mac平台.

Trying to add multiple items to a scrollpane I quickly found that all "addActor" functions are Unsupported. So, I went with adding a table with all the items I wanted (this code misses a image that I still want to add) to make a scrollable credits screen... but this approach (currently) doesn't allow for overflow, rendering the ScrollPane useless. (My text is only shown up to what the screen's height allows, and isn't scrollable). What's the way to make a scrollable pane with multiple widgets in LibGDX? (I only care about Android and Win/Lin/Mac platforms at the moment.

    pane = new ScrollPane(null, skin);
    pane.setFillParent(true);
    paneContent = new Table(skin);
    paneContent.setFillParent(true);
    Label temp = new Label("", skin);
    temp.setAlignment(Align.left, Align.center);
    temp.setText( Gdx.files.internal("licenses/credits.txt").readString("UTF-8") );
    paneContent.addActor(temp);
    pane.setWidget(paneContent);
    stage.addActor(pane);

推荐答案

如果你想将多个项目放入 ScrollPane 中,你只需要在其中放入一个表格,然后为要放入的每个小部件调用 add()滚动窗格.

If you want to put multiple items into the ScrollPane you simply need to put a table into it and call add() for each widget you want to put into the ScrollPane.

以下是如何使您的信用可滚动的示例:

Below is an example of how to make your credits scrollable:

public class ScrollTest implements ApplicationListener {
    private Stage stage;

    private static final String reallyLongString = "This
Is
A
Really
Long
String
That
Has
Lots
Of
Lines
And
Repeats.
"
        + "This
Is
A
Really
Long
String
That
Has
Lots
Of
Lines
And
Repeats.
"
        + "This
Is
A
Really
Long
String
That
Has
Lots
Of
Lines
And
Repeats.
";

    @Override public void create() {
        this.stage = new Stage();
        Gdx.input.setInputProcessor(this.stage);
        final Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

        final Label text = new Label(reallyLongString, skin);
        text.setAlignment(Align.center);
        text.setWrap(true);
        final Label text2 = new Label("This is a short string!", skin);
        text2.setAlignment(Align.center);
        text2.setWrap(true);
        final Label text3 = new Label(reallyLongString, skin);
        text3.setAlignment(Align.center);
        text3.setWrap(true);

        final Table scrollTable = new Table();
        scrollTable.add(text);
        scrollTable.row();
        scrollTable.add(text2);
        scrollTable.row();
        scrollTable.add(text3);

        final ScrollPane scroller = new ScrollPane(scrollTable);

        final Table table = new Table();
        table.setFillParent(true);
        table.add(scroller).fill().expand();

        this.stage.addActor(table);
    }

    @Override public void render() {
        this.stage.act();
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        this.stage.draw();
    }

    @Override public void resize(final int width, final int height) {}
    @Override public void pause() {}
    @Override public void resume() {}
    @Override public void dispose() {}
}

添加了在 ScrollPane 中设置表格的代码.

Added code on setting up a table inside of a ScrollPane.

相关文章