在表格行中添加的图像会占用更多的行空间

时间:2019-09-06 14:17:52

标签: java user-interface libgdx

我在桌子上添加了一些图像,这些图像的侧面有标签,但是图像将标签推得很远,从而扭曲了整体布局。

图像的分辨率为640x640。

布局:enter image description here

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

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

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

我想为图像找到一种不跨越如此宽广的区域的方法(红线应仅封装图像而不能超出图像范围)

非常感谢您!

1 个答案:

答案 0 :(得分:0)

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

由于Table分配列的额外空间的方式,这不一定将所有内容居中。如果您需要将所有内容居中,那么实际上您将需要一个内部表来存储除“后退”按钮之外的所有内容。