如何获得带有包装文本的标签?

2022-01-12 00:00:00 label java libgdx

我试过以下代码:

Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label);
...

但我得到的只是一条很宽的线,从屏幕上划出.如何获取带有换行文本的标签?

and yet all I get is a very wide line that draws off the screen. How to get a Label with wrapped text?

推荐答案

这与 slider always has default width 或 "滑块始终具有默认宽度".

您需要将该标签放入表格中,并将正确大小添加到标签所在表格的单元格中.

You need to put that label into a table and add the right size to the cell of the table where the label is.

UI 小部件不设置自己的大小和位置.相反,父窗口小部件设置每个孩子的大小和位置.小部件提供父级可以用作提示的最小、首选和最大大小.一些父窗口小部件,例如 Table,可以限制子窗口的大小和位置.要在布局中为小部件指定特定尺寸,小部件的最小、首选和最大尺寸将保持不变,并在父级中设置尺寸限制.

UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.

来源:来自 libgdx wiki Scene2D

解决办法:

Label label = new Label(reallyLongString, skin);
label.setWrap(true);
label.setWidth(100); // or even as low as 10
table.add(label).width(10f);// <--- here you define the width

相关文章