如何在scrollPane顶部对齐表格?

时间:2015-02-21 22:39:35

标签: libgdx stage

我正在我的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);

2 个答案:

答案 0 :(得分:0)

我今天遇到了同样的问题,并在这里碰到过,所以最好还是在这里发布答案,以防有人还在寻找答案。

@peterSweter说top()不起作用,@ Rara的建议与调用top相同。

获得类似于顶部对齐的唯一方法(仅在innerTable较小时才有意义)是通过在其周围添加空元素来增加大小,因此传递给ScrollPane的表的位置为至少与scrollPane相同。

我只是将另一个Table包裹在我的innerTable引用周围。 (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);

答案 1 :(得分:0)

为了实现所需的功能,可以将表包装在另一个表中,并将该表用于ScrollPane。请注意,将表添加到容器表时,需要在底部和右侧添加一些可增长的空单元格,以便将其“推”到左上角(尽管我相信您也可以只使用containerTable.add (myTable).top()。left()-但尚未进行实际测试)。 当然,这比使用硬编码高度的second解决方案好得多。事实是,您通常希望将滚动窗格添加为可增长(.grow()),因此固定的高度/宽度是无用的。

相关问题