【问题标题】:trying to set the text above the Image in ImageTextButton on libGdx试图在 libGdx 上的 ImageTextButton 中设置图像上方的文本
【发布时间】:2015-09-23 17:48:18
【问题描述】:

我正在使用 LIBGDX。

我正在尝试在图像的中心设置一个文本字符串,并将其用作可点击按钮。

不幸的是,我没有找到解决方案。

【问题讨论】:

  • 能否发布您到目前为止所尝试的内容?
  • ImageTextButton.ImageTextButtonStyle style = new ImageTextButton.ImageTextButtonStyle(startButtonStyle); final ImageTextButton bb = new ImageTextButton(Integer.toString(levelNumber), style ); bb.stack(new Image(new TextureRegion(new Texture(Gdx.files.internal("levelButton.png"))))); bb.getLabel().setAlignment(Align.center); 还是不行
  • 如果您不知道如何使用小部件,最好的方法是使用TableLabel。我必须说,libgdx 的 UI 有点难以按预期工作如果你是新手,所以尝试简单的方法,并用你的逻辑控制点击事件来改变图像。如果你做到了,你可以实现更复杂的组件,比如ImageTextButton

标签: libgdx imagebutton


【解决方案1】:

是的,您可以使用 getLabel 方法:

myButton.getLabel().setAlignment(Align.center);

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Align.html

【讨论】:

    【解决方案2】:

    默认情况下,ImageTextButton 在 1 行中包含 2 个 Actor:Image 和 Label。要更改标签的位置,可以在创建 ImageTextButton 之后重建底层表格单元格:

      new ImageTextButton(...) {
        val label = getLabel
        val image = getImage
        clearChildren()
        add(image)
        row() // second row
        add(label)
      }
    

    现在标签将位于图像下方。代码是 Scala。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      相关资源
      最近更新 更多