【问题标题】:How do you scale a horizontal group inside a table如何在表格内缩放水平组
【发布时间】:2016-02-10 17:53:17
【问题描述】:

我有一个HorizontalGroup,其中有两个文本按钮。现在我想将这些按钮缩放到它们的一半大小,因为它们太大了,但我还没有找到一个好的方法来做到这一点,我尝试了一些方法,但它不是我想要的。

下面是设置代码

    weaponsButton = new TextButton("Weapons", buttonStyle);
    weaponsButton.getLabel().setColor(Color.BLACK);
    weaponsButton.pad(10f);
    statsButton = new TextButton("Stats", buttonStyle);
    statsButton.getLabel().setColor(Color.BLACK);
    statsButton.pad(10f);

    hGroup = new HorizontalGroup();
    hGroup.addActor(weaponsButton);
    hGroup.addActor(statsButton);

    table = new Table();
    table.add(hGroup);

    table.setFillParent(true);
    table.debug();

    stage = new Stage();
    stage.addActor(table);

buttonStyle 只是一个TextButtonStyle 与一个NinePatchDrawable 和一个BitmapFont。当您绘制表格时,它看起来像这样:

添加hGroup.setScale(0.5f); 看起来像这样:

现在hGroup 未居中对齐,.aling(Align.center) 在添加 hGroup 时不执行任何操作。

在添加hGroup 以缩小单元格之后调用.width(float) 似乎也没有用,因为我不知道缩放hGroup 的确切宽度,它只给出未缩放的宽度,我觉得它有点笨重得到宽度,然后乘以缩放因子。

即使我这样做了,它看起来也不是我想要的。

table.add(hGroup).width(hGroup.getPrefWidth()*0.5f).height(hGroup.getPrefHeight()*0.5f);

看起来像这样:

感谢任何建议

【问题讨论】:

    标签: java user-interface libgdx scene2d


    【解决方案1】:

    根据documentation,Horizo​​ntalGroup 的宽度是其所有子项(您的按钮)的首选宽度之和。鉴于此,您有几个选择:

    1. 在您的按钮演员上调用 setWidth 并给他们一个宽度。
    2. 跳过 Horizo​​ntalGroup,直接将按钮统一添加到表格中:

      Table table = new Table();
      table.defaults().uniformX();
      table.add(weaponsButton);
      table.add(statsButton);
      

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2015-09-20
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多