【问题标题】:How to align table in scrollPane top?如何在 scrollPane 顶部对齐表格?
【发布时间】:2015-04-23 12:07:07
【问题描述】:

我正在我的 libgdx 游戏中制作成就屏幕。我想将 scrollPane 中的表格对齐到顶部,现在它垂直和水平居中,我不知道为什么。当我尝试创建新行时使用 .top() 方法不起作用。

stage      = new Stage();
    Gdx.input.setInputProcessor(stage);
    outerTable = new Table();
    innerTable = new Table();



    innerTable.setFillParent(true);



    for(int i=0;i<score_bound; i++){

        if(i<score_state){
         innerTable.row().width(achie_width).height(achie_height).top();

         innerTable.add(new Image(ltm.assets.score_textures[i]));
        }else{

            innerTable.row().width(achie_width).height(achie_height).top();

             innerTable.stack(new Image(ltm.assets.score_textures[i]),new Image(ltm.assets.kurtyna));

        }

    }

    for(int i=0;i<letin_bound; i++){

        if(i<letin_state){
             innerTable.row().width(achie_width).height(achie_height).top();

             innerTable.add(new Image(ltm.assets.letin_textures[i]));
            }else{

                innerTable.row().width(achie_width).height(achie_height).top();

                 innerTable.stack(new Image(ltm.assets.letin_textures[i]),new Image(ltm.assets.kurtyna));

            }

    }

    scrollPane = new ScrollPane(innerTable); 


    outerTable.setPosition(0, 0);
    outerTable.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());



    outerTable.debug();

    outerTable.add(scrollPane).fill().expand();


    stage.addActor(outerTable);

【问题讨论】:

  • 尝试将outerTable.add(scrollPane).fill().expand();改为outerTable.add(scrollPane).fill().expand().align(Align.top);

标签: libgdx stage


【解决方案1】:

我今天遇到同样的问题并在这里遇到了,所以我不妨在这里发布一个答案,以防有人仍在寻找答案。

@peterSweter 说 top() 不起作用,@Rara 建议的和调用 top 一样。

实现看起来像顶部对齐的唯一方法 - 仅当 innerTable 较小时才相关 - 是通过在其周围添加空元素来增加大小,因此您传递给 ScrollPane 的表格有至少与 scrollPane 的大小相同。

我刚刚在我的 innerTable 引用周围包裹了另一个 Table。 (658 是我用于 ScrollPane 的固定高度)

    Table spacingTable = new Table();
    spacingTable.setHeight(Math.max(innerTable.getHeight(), 658));
    spacingTable.add(innerTable);

    if (innerTable.getHeight() < 658) {
        spacingTable.row();
        spacingTable.add().expandY();
    }

    ScrollPane scroll = new ScrollPane(spacingTable, skin);

【讨论】:

    【解决方案2】:

    为了实现您想要的,您可以将您的表格包装在另一个表格中,并将该表格用于 ScrollPane。请注意,将表格添加到容器表格时,您需要在底部和右侧添加一些可增长的空单元格,以便将其“推”到左上角(尽管我相信您也可以简单地使用 containerTable.add (myTable).top().left() - 但还没有真正测试过)。 这当然是比使用硬编码高度的第二个解决方案更好的解决方案。问题是,您通常希望将滚动窗格添加为可增长的 (.grow()),因此固定的高度/宽度是无用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2020-06-17
      • 2011-12-30
      • 2013-10-02
      • 2015-02-19
      • 2016-08-26
      相关资源
      最近更新 更多