Java 如何更改 libgdx 标签中背景的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18166556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the color of the background in libgdx labels?
提问by
I can change the color of the font like this
我可以像这样改变字体的颜色
LabelStyle style1 = new LabelStyle(..some font...,
Color.valueOf("FF4500")
);
label.setStyle(style1);
but how do I change the background? right now the background is the same as the background of whole screen which is set in
但我如何改变背景?现在背景与设置的全屏背景相同
render method lke this
像这样的渲染方法
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(
1.000f, 0.980f, 0.941f
,1);
回答by BennX
Actually you do not change the background of the Lable like that. You did just change the clearcolour. Guess you know that.
实际上你不会像那样改变标签的背景。你只是改变了clearcolor。猜你知道。
To change the background you need to change the background at the style of the label. To do so i'd recommend to use a simple NinePatch
as background, (can be a square! if its white you can change the colour of the ninepatch and the background colour changes!)
要更改背景,您需要更改标签样式的背景。为此,我建议使用简单的NinePatch
作为背景,(可以是正方形!如果它是白色的,您可以更改 Ninepatch 的颜色并更改背景颜色!)
NinePatch temp = new NinePatch(new Texture(....), 10, 10, 10, 10); //edges
For more information about ninepatch take a look here libgdx wiki ninepatch
有关 Ninepatch 的更多信息,请查看此处的 libgdx wiki Ninepatch
You needto add that ninepatch to an Skin objekt. For example like this
您需要将该 Ninepatch 添加到 Skin 对象。例如像这样
Skin skin = new Skin();
skin.add("background",temp)
After that you can get a drawable from the skin that you can set as background of the LabelStyle
.
之后,您可以从皮肤中获取可绘制对象,您可以将其设置为LabelStyle
.
style1.background = skin.getDrawable("background");
You can also use a simple bitmap but that does get scaled to the label size which causes in most of the cases deformation. A Ninepatch can be scaled without having deformation.
您也可以使用简单的位图,但它确实会缩放到在大多数情况下会导致变形的标签大小。Ninepatch 可以缩放而不会变形。
回答by jwehrle
Label label = new Label(labelText, skin);
Pixmap labelColor = new Pixmap(labelWidth, labelHeight, Pixmap.Format.RGB888);
labelColor.setColor(<your-color-goes-here>);
labelColor.fill();
label.getStyle().background = new Image(new Texture(labelColor)).getDrawable();
Basically, use the getDrawable() function of the Image class to assign the color of your Label's LabelStyles' background Drawable.
基本上,使用 Image 类的 getDrawable() 函数来指定 Label 的 LabelStyles 的背景 Drawable 的颜色。
This is the simplest workaround I've been able to come up with and, frankly, it's just silly that there is no setBackground() in the Label class.
这是我能想到的最简单的解决方法,坦率地说,在 Label 类中没有 setBackground() 是愚蠢的。
Actually, maybe the easiest fix is to hack the Label class and add a setBackground() method to it.
实际上,也许最简单的解决方法是破解 Label 类并向其添加 setBackground() 方法。
[Edit] Be sure to dispose of Pixmaps when you are done with them; i.e. labelColor.dispose();
[编辑] 处理完像素图后,请务必处理它们;即 labelColor.dispose();
[Update] @Mitrakov Artem made a good point: The above solution will affect all instances of this LabelStyle. If that's not what you want you can create a new LabelStyle, use the above method on it, then save it to the Label. Quoting Artem: "So I would recommend to create a new style (LabelStyle style = new LabelStyle(label.getStyle());), change its background and then apply it to the label (label.setStyle(style);)"
[更新] @Mitrakov Artem 提出了一个很好的观点:上述解决方案将影响此 LabelStyle 的所有实例。如果这不是您想要的,您可以创建一个新的 LabelStyle,对它使用上述方法,然后将其保存到 Label。引用 Artem:“所以我建议创建一个新样式(LabelStyle style = new LabelStyle(label.getStyle());),更改其背景,然后将其应用于标签(label.setStyle(style);)”
回答by Steffen K
If you need a quick and easy solution, you can use the snippet below. It doesn't work well with multiline text because it doesn't take the text width per line into account. Anyway, the background is automatically adjusted to the width and height of the label widget (i.e. if your text changes).
如果您需要快速简便的解决方案,可以使用下面的代码段。它不适用于多行文本,因为它没有考虑每行的文本宽度。无论如何,背景会自动调整为标签小部件的宽度和高度(即,如果您的文本发生变化)。
private Label label = new Label("text", createLabelStyleWithBackground());
private LabelStyle createLabelStyleWithBackground() {
LabelStyle labelStyle = new LabelStyle();
labelStyle.font = new BitmapFont();
labelStyle.fontColor = Color.WHITE;
labelStyle.background = createBackground();
return labelStyle;
}
private Drawable createBackground() {
Pixmap labelColor = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
Color color = new Color(Color.GRAY);
color.a = 0.75f;
labelColor.setColor(color);
labelColor.fill();
Texture texture = new Texture(labelColor);
return new BaseDrawable() {
@Override
public void draw(Batch batch, float x, float y, float width, float height) {
GlyphLayout layout = label.getGlyphLayout();
x = label.getX();
y = label.getY() - (layout.height + 15) / 2; // +15 is some space
batch.draw(texture, x, y, layout.width, layout.height + 15);
}
};
}