How to change the size of an existing scene2d.ui widget in a window/table?

萝らか妹 提交于 2019-12-05 01:47:57
BennX

I had the same Problem and it took a while to get the solution. Take a look at this quastion slider always has default width.

The solution is:

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.
Layout at Wiki

From the wiki of libGDX. They just added it. So while you add those widgets to the table just add the line .width(newTableWidth) at the end and it should be at the right width.

table.add(textField).width(600).height(60);

Based on BeenX's answer, it's evident that the size is set on the cell of the table that contains the widget, rather than on the widget itself. The table.add() method returns a cell, so when running table.add(textField).width(600).height(60) the width and height is being applied to this cell.

Hence my solution to update an already existing cell was to find the cell I want to update with table.getCells() then apply the new width on that. The example below changes the width of the last cell in the list.

List<Cell> cells = window.getCells(); 
Cell cell = cells.get(cells.size()-1);
cell.width(newWidth);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!