简体   繁体   English

Libgdx 表太小,无法填充视口

[英]Libgdx Table too small, does not fill viewport

I'm trying to use a Table to create a menu with lined-up Buttons and Labels.我正在尝试使用表格来创建一个带有排列的按钮和标签的菜单。 I'm using the latest nightly builds of libgdx, which changed the Table API (amongst other things).我正在使用 libgdx 的最新夜间版本,它改变了 Table API(除其他外)。

Here's the code in my Screen constructor:这是我的 Screen 构造函数中的代码:

this.mStage = new Stage();
this.mTable = new Table();
this.mTable.setFillParent(true);
this.mTable.debugTable();
this.mStage.addActor(this.mTable);

// now I add some stuff to the table
// ...

My resize function looks like this:我的resize功能如下所示:

this.mStage.setViewport(width, height, true);
this.mTable.setFillParent(true);
this.mTable.invalidate();

My render function has the following:我的render功能有以下几点:

System.out.println("Table size is " + 
    this.mTable.getWidth() + " by " + 
    this.mTable.getHeight());
this.mStage.draw();
Table.drawDebug();

Although my console shows the correct size:虽然我的控制台显示正确的大小:

Table size is 480.0 by 640.0

the Table size is shrink-wrapped around its children, and does not extend to the correct size of 480x640.表格大小被收缩包裹在其子项周围,并且不会扩展到 480x640 的正确大小。

Any ideas?有任何想法吗?

Answering my own question for the benefit of others.为了他人的利益,回答我自己的问题。 The way I was adding widgets to the table was incorrect:我向表格添加小部件的方式不正确:

this.mTable.row();
this.mTable.add(button).fillX();

I needed to call the fillX() method of the row() :我需要调用fillX()的方法row()

this.mTable.row().fillX();
this.mTable.add(button);

Hope this helps someone.希望这可以帮助某人。

Your incorrect version of adding widget to the table can be easily corrected by expanding a cell first using .expandX() method and then by filling that cell with a widget by calling fillX() .通过首先使用.expandX()方法扩展单元格,然后通过调用fillX()用小部件填充该单元格,可以轻松更正将小部件添加到表格中的错误版本。 so your initial code:所以你的初始代码:

this.mTable.row();
this.mTable.add(button).fillX();

should be corrected to the following version:应更正为以下版本:

this.mTable.row();
this.mTable.add(button).expandX().fillX();

these 2 lines can be shorten to one:这两行可以缩短为一行:

this.mTable.add(button).expandX().fillX().row();

To add to the above answers, it may be helpful to know that you can make these setting defaults for the table like so:要添加到上述答案中,知道您可以像这样为表设置这些设置默认值可能会有所帮助:

this.mTable.defaults().expandX().fillX();

The settings will apply uniformly to all rows and cells.这些设置将统一应用于所有行和单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM