繁体   English   中英

更改 Libgdx 按钮外观

[英]Changing Libgdx Button appearance

当我的某些事件被触发时,我需要能够更改 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();

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

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

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

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

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

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

我设法让这个工作。 在我的 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()。

改用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");

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM