java 带有多个小部件的 LibGDX 和 ScrollPane

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15484077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:45:12  来源:igfitidea点击:

LibGDX and ScrollPane with multiple widgets

javaandroidlibgdx

提问by efaj

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.

尝试向滚动窗格中添加多个项目时,我很快发现所有“addActor”功能均不受支持。因此,我添加了一个包含我想要的所有项目的表格(此代码遗漏了我仍然想添加的图像)以制作可滚动的积分屏幕……但这种方法(目前)不允许溢出、渲染ScrollPane 没用。(我的文字仅显示到屏幕高度允许的范围内,并且不可滚动)。在 LibGDX 中使用多个小部件制作可滚动窗格的方法是什么?(我目前只关心 Android 和 Win/Lin/Mac 平台。

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

回答by Jyro117

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.

如果您想将多个项目放入 ScrollPane,您只需将一个表格放入其中,并为您想要放入 ScrollPane 的每个小部件调用 add()。

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\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n";

    @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() {}
}

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

编辑:添加了在 ScrollPane 内设置表格的代码。