首页 > 解决方案 > 在表格行中添加的图像占用了更多的行空间

问题描述

我在表格中添加了一些带有标签的图像,但是图像将标签推得很远,因此扭曲了整体布局。

图像分辨率为 640x640。

布局:在此处输入图像描述

我尝试使用单元格函数并尝试填充不同的行和列(有点工作,但这不是我想要的,因为它取代了整个表格的组件)并且我已经阅读了 GitHub 中的表格文档,但是我仍然遇到同样的问题。

这是表格布局代码(在类构造函数中初始化的表格)

        int[] values;
        ArrayList<Label> labels = new ArrayList<>();
        //just setting up labels with according values, not really important
        if (label.getText().toString().contains("Conjurer")) values = new int[]{200, 9, 10, 15, 15};

        else if (label.getText().toString().contains("Monk")) values = new int[]{300, 14, 15, 11, 8};

        else values = new int[]{320, 14, 17, 9, 13};
        //creating new labels and adding to list
        for (int i = 0; i <= 4; i++) {
            Label l = new Label(String.valueOf(values[i]), UI.createNewLabelStyle(font, Color.WHITE)); //temp label to add
            labels.add(l);
        }

        Image healthIcon = new Image(stat_atlas.findRegion("healthIcon"));
        Image defenseIcon = new Image(stat_atlas.findRegion("defenseIcon"));
        Image attackIcon = new Image(stat_atlas.findRegion("attackIcon"));
        Image magicAttackIcon = new Image(stat_atlas.findRegion("magicAttackIcon"));
        Image magicDefenseIcon = new Image(stat_atlas.findRegion("magicDefenseIcon"));

        table.setDebug(true);
        table.row();
        table.add(healthIcon).height(60f).width(60f);
        table.add(labels.get(0));
        table.row();
        table.add(attackIcon).height(60f).width(60f).right();
        table.add(labels.get(1));
        table.row();
        table.add(defenseIcon).height(60f).width(60f).right();
        table.add(labels.get(2));
        table.row();
        table.add(magicAttackIcon).height(60f).width(60f).right();
        table.add(labels.get(3));
        table.row();
        table.add(magicDefenseIcon).height(60f).width(60f).right();
        table.add(labels.get(4));
        table.row();

我想要的是找到一种方法让图像不跨越这么大的区域(红线应该只封装图像而不超出它)

太感谢了!

标签: javauser-interfacelibgdx

解决方案


发生的事情是您的后退按钮只占用一列,因此第二列中的所有标签都必须出现在它的右侧。添加后退按钮时,调用colSpan(2)返回的单元格。此外,当您添加标签时,您可能需要调用left()它们返回的单元格,以便它们推送图像。

这不一定会使所有内容居中,因为 Table 分配列的额外空间的方式。如果您需要全部居中,则实际上除了“后退”按钮之外,您还需要一个内表来存放所有内容。


推荐阅读