更改Libgdx按钮外观

Rap*_*ard 3 java android button drawable libgdx

Button当我的一些事件被触发时,我需要能够改变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();
Run Code Online (Sandbox Code Playgroud)

我做了很多搜索,但我找不到答案.有人能告诉我这样做的正确方法吗?

P.T*_*.T. 5

我认为问题是你在初始化之后改变了皮肤/样式的内容Button,但是按钮对象在初始化之后没有引用样式.我也不完全清楚你的目标是什么.我假设您要显示"登录"按钮,直到用户登录,然后您希望该按钮成为"退出"按钮.

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

也许尝试强迫风格setStyle,像这样:

   googleButton.getStyle().up = ... whatever;
   googleButton.setStyle(googleButton.getStyle());
Run Code Online (Sandbox Code Playgroud)

也就是说,我认为你最好有两个(?)不同的Button对象(一个用于登录,一个用于登出).将它们装在一个堆栈中,然后让一个或另一个看不见(参见参考资料Actor.setVisible)