【问题标题】:Changing Libgdx Button appearance更改 Libgdx 按钮外观
【发布时间】:2013-06-27 03:46:43
【问题描述】:

当我的某些事件被触发时,我需要能够更改 Libgdx Button 的外观。

这是我的按钮的声明:

Button googleButton = new Button(skin.getDrawable("google_sign_in"));

这是我尝试过的事情:

googleButton.setBackground(skin.getDrawable("google_sign_out"));
googleButton.invalidate();

googleButton.setChecked(true);
googleButton.invalidate();

googleButton.getStyle().up = skin.getDrawable("google_sign_out");
googleButton.invalidate();

我进行了很多搜索,但找不到答案。有人可以告诉我这样做的正确方法吗?

【问题讨论】:

  • 问题是我不希望它在上下事件时改变它的外观,它是没有与按钮本身链接的事件需要切换其图像。跨度>
  • 我需要能够更改图片,而不是相同。也许我不需要使用按钮,我应该使用什么类型的演员才能动态更改其图像?
  • 您也可以发布“google_sign_in”样式吗?
  • 这不是一种样式,它是一个可绘制对象。

标签: java android button drawable libgdx


【解决方案1】:

我认为问题在于您在初始化Button 后更改了皮肤/样式的内容,但按钮对象在初始化后没有引用样式。我也不完全清楚你要做什么。我假设您想显示一个“登录”按钮,直到用户登录,然后您希望该按钮成为一个“退出”按钮。

invalidate 方法只是触发重新布局(重新计算Actor 的大小/位置)。

也许尝试使用setStyle 强制样式,如下所示:

   googleButton.getStyle().up = ... whatever;
   googleButton.setStyle(googleButton.getStyle());

也就是说,我认为您最好拥有两个 (?) 不同的 Button 对象(一个用于登录,一个用于注销)。将它们打包在Stack 中,然后让其中一个或另一个不可见(参见Actor.setVisible

【讨论】:

  • 我已经尝试在初始化后设置样式,但它什么也没做。真诚地说,堆栈完全没用,它只是一个向量,它不会改变其中元素的 z 索引......我设法通过像你建议的那样切换两个按钮的可见性来改变按钮的可见性。我只是希望只有一个演员可以做到这一点。
【解决方案2】:

我设法让它工作。在我的 Actor 中,一个有 6 个可能图像的骰子:

public Dice(int options) {
    super(new Button.ButtonStyle());
}

而当我想换脸时:

public void setValue(int value) {
    this.value = value;
    Button.ButtonStyle style = getStyle();
    style.up = new TextureRegionDrawable(
            Assets.instance.dices.facesUp[value]);
    style.down = new TextureRegionDrawable(
            Assets.instance.dices.facesDown[value]);
    style.checked = new TextureRegionDrawable(
            Assets.instance.dices.facesChecked[value]);
    setSize(getPrefWidth(), getPrefHeight());
}

我认为诀窍在于

setSize(getPrefWidth(), getPrefHeight());

如果我省略它,则不会显示骰子。并且不需要 setStyle 或 invalidate()。

【讨论】:

    【解决方案3】:

    改用CheckBox 类;

    1- 为皮肤制作一个 .json 文件

    {
    com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: fontfile.fnt } },
    com.badlogic.gdx.graphics.Color: {
        green: { a: 1, b: 0, g: 1, r: 0 },
        white: { a: 1, b: 1, g: 1, r: 1 },
        red: { a: 1, b: 0, g: 0, r: 1 },
        black: { a: 1, b: 0, g: 0, r: 0 }
      },
    com.badlogic.gdx.scenes.scene2d.ui.CheckBox$CheckBoxStyle: {
        google-loggin: { checkboxOn: google_sign_in, checkboxOff: google_sign_out, font: default-font, fontColor: white }
    },
    }
    

    2- 制作皮肤

    skin = new Skin(Gdx.files.internal("your.json-file.json"), new TextureAtlas("textureAtlas-file.pack"));
    

    3- 创建忽略第一个参数的按钮:

    CheckBox loginButton = new CheckBox("", skin, "google-loging");
    

    【讨论】:

      【解决方案4】:

      仅适用于通过 google 搜索此内容的任何人。在已经初始化之后,从已经添加到皮肤文件的样式更改按钮的样式:

      btn.setStyle(skin.get("YOUR BTN STYLE",TextButton.TextButtonStyle.class));
      

      【讨论】:

        【解决方案5】:

        您必须将图像添加到按钮button_1.addActor(image)。此图像将覆盖按钮源皮肤。然后你可以关闭再打开这张图片image.setVisible(false)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-14
          • 2010-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多