【问题标题】:LibGdx: Table not displayed correctlyLibGdx:表格未正确显示
【发布时间】:2014-02-27 07:57:29
【问题描述】:

我在 libgdx 中有一个表,它应该有 6 个按钮,分为 2 行。这是我的代码:

        elementsTable.clear();
        String[] colors = new String [6];
        colors[0] = "red";
        colors[1] = "orange";
        colors[2] = "yellow";
        colors[3] = "purple";
        colors[4] = "blue";
        colors[5] = "green";

        String category = currentCategory.name();
        category = category.substring(0, category.length()-1).toLowerCase(); //removes the last character because otherwise there would be an extra 's'

        int count = 0;
        for(String color : colors) {
            ButtonStyle buttonStyle = new ButtonStyle();
            buttonStyle.up = cellsSkin.getDrawable(category + "_" + color);
            Button button = new Button(buttonStyle);
            button.addListener(toolBoxListener);
            button.setName(category + "_" + color);
            button.setSize(toolBoxButtonSize, toolBoxButtonSize);
            elementsTable.add(button).pad(toolBoxButtonPadding);

            count ++;
            if (count == Math.ceil((colors.length/2d))) elementsTable.row();
        }

这就是填充物。我的按钮有 6 种不同的颜色,所以我用颜色名称遍历数组以访问皮肤以获取按钮的可绘制对象。这部分似乎有效,因为当我调试并查看表格时,它有 6 个大小合适的按钮。

然后我设置我的桌子的位置和大小并将它添加到舞台上。:

    elementsTable.setSize(toolBoxWidth, 500/(float)(3*toolBoxButtonSize+6*toolBoxButtonPadding)*elementsTable.getHeight());
    elementsTable.setPosition(195, 30);
    stage.addActor(elementsTable);

舞台有一个正交相机组,可以很好地缩小物体。

但是,我得到的是:

另一个奇怪的问题是,当我在调试模式下查看表格的高度和宽度时,它显示为 0,即使其中的元素都具有正确的大小。

谁能帮帮我?

如果您对此问题还有任何疑问,请提出!我试图给出详细的描述,但我很乐意回答您的问题。

【问题讨论】:

    标签: java android layout libgdx


    【解决方案1】:

    这似乎只是位置。 30 对 Y 来说太低了:

    elementsTable.setPosition(195, 30);
    

    例如尝试 150:

    elementsTable.setPosition(195, 150);
    

    记住 Libgdx(和 OpenGL)使用的坐标系是 Y 向上。

    【讨论】:

    • 不要忘记setPosition(float x, float y) 确实将表格的中心设置为给定位置。所以 30 意味着表格的中心在屏幕底部上方 30px 处。我建议将其简单地以table.setPosition(cam.viewportWidth / 2,cam.viewportHeight / 2); 居中以测试其是否为表格
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2013-03-21
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多