简体   繁体   English

更改 Libgdx 按钮外观

[英]Changing Libgdx Button appearance

I need to be able to change the appearance of a Libgdx Button when some of my events are triggered.当我的某些事件被触发时,我需要能够更改 Libgdx Button的外观。

Here is the declaration of my button:这是我的按钮的声明:

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

Here are the things I tried:以下是我尝试过的事情:

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

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

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

I have done a lot of searches but I can't find the answer.我已经做了很多搜索,但我找不到答案。 Could somebody show me the right way of doing this?有人可以告诉我这样做的正确方法吗?

I think the problem is that you're changing the contents of the skin/style after initializing the Button , but the button object does not reference the style after it has been initialized.我认为问题在于您在初始化Button后更改了外观/样式的内容,但是按钮对象在初始化后并未引用该样式。 I'm also not entirely clear on what you're going for.我也不完全清楚你要做什么。 I assume you want to show a "sign in" button, until the user is signed in, and then you want that button to be a "sign out" button.我假设您想显示“登录”按钮,直到用户登录,然后您希望该按钮成为“退出”按钮。

The invalidate method just triggers a re-layout (to re-compute the size/location of the Actor ). invalidate方法只是触发重新布局(重新计算Actor的大小/位置)。

Maybe try forcing the style with setStyle , like this:也许尝试使用setStyle强制样式,如下所示:

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

That said, I think you would be better off having two (?) different Button objects (one for logging in, and one for logging out).也就是说,我认为您最好拥有两个 (?) 不同的Button对象(一个用于登录,一个用于注销)。 Pack them in a Stack , and just make one or the other invisible (see Actor.setVisible )将它们打包成一个Stack ,然后让其中一个不可见(参见Actor.setVisible

I managed to get this to work.我设法让这个工作。 In my Actor, a dice that have 6 possible images:在我的 Actor 中,一个骰子有 6 个可能的图像:

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

And when I want to change the face:当我想换脸时:

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

I think the trick is at the我认为诀窍在于

setSize(getPrefWidth(), getPrefHeight());

If I omit it, the dice doesn't get displayed.如果我省略它,则不会显示骰子。 And there's no need for setStyle or invalidate().并且不需要 setStyle 或 invalidate()。

Use CheckBox class instead;改用CheckBox类;

1- make a .json file for the skin 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- make a skin 2-制作皮肤

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

3- create the button ignoring the first argument: 3- 创建按钮忽略第一个参数:

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

Just for anyone searching for this through google.仅适用于通过谷歌搜索此内容的任何人。 To change the style of the button from a style already added to your skin file, after already initialising:在已经初始化之后,从已经添加到皮肤文件的样式更改按钮的样式:

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

You must add image to button button_1.addActor(image) .您必须将图像添加到按钮button_1.addActor(image) This image will cover the button origin skin.此图像将覆盖按钮原始皮肤。 And then you can switch off and on this image image.setVisible(false)然后你可以关闭和打开这个图像image.setVisible(false)

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

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