当我的某些事件被触发时,我需要能够更改 Libgdx 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
)
关于java - 更改 Libgdx 按钮外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17334198/