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

强颜欢笑 提交于 2019-12-22 03:52:51

问题


I'm attempting to resize a scene2d.ui window (which is basically a table that can be moved around) and its contents. So far I can only successfully change the window size itself, but not its contents. I'm doing it like this:

window.setWidth(newTableWidth);
textField.setWidth(newFieldWidth);
window.invalidate(); <--- New size of window is drawn properly when invalidate() is called
textField.invalidate(); <--- New textField size is NOT drawn, however

In this case, the window contains a text field which I'm trying to resize along with the window, but only the window gets redrawn with its new size while the text field remains the same.

How do I properly update the widgets within the window?


回答1:


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);



回答2:


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);


来源:https://stackoverflow.com/questions/18539037/how-to-change-the-size-of-an-existing-scene2d-ui-widget-in-a-window-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!