单击时更改TextButton的字体颜色?

时间:2015-08-06 13:04:19

标签: libgdx scene2d

我已经阅读了有关scene2d的UI功能的一些教程和文档,但我无法弄清楚外观是如何工作的。我只想要一个TextButton,在按下时会改变颜色。我设法让TextButton的背景改变,但那不是我想要的。它应该没有背景。

如果有人能提供一个如何做到这一点的例子,我将非常感激。看起来很简单,所以我觉得我在这里遗漏了一些明显的东西。如果涉及皮肤,请以编程方式编写。

谢谢。

2 个答案:

答案 0 :(得分:7)

事实证明它就像我想的那么简单,当我第一次尝试时它就没用了。

定义TextButtonStyle时,您可以为文本分配downFontColor所需的颜色。您还可以指定checkedFontColor按下时希望文本切换的颜色。

示例:

TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.font = someBitmapFont;
textButtonStyle.fontColor = Color.WHITE;
textButtonStyle.downFontColor = Color.BLACK;
//Optional color to toggle between when pressed
textButtonStyle.checkedFontColor = Color.GREEN;
final TextButton textButton = new TextButton("Text", textButtonStyle);

这将产生一个白色TextButton,按下时会变黑。释放鼠标/触摸时,它将变为绿色或白色,具体取决于切换的状态。

答案 1 :(得分:0)

您也可以对其进行着色,但是效果取决于基色(如果为白色,则效果很好,否则取决于)。无论如何,都是这样的:

button.getLabel().setColor(myColor);
相关问题