libgdx 选择框适合内容大小

libgdx selectbox fit content size

我尝试创建一个根据内容调整宽度的选择框。 JavaDoc 指出:The preferred size of the select box is determined by the maximum text bounds of the items and the size of the SelectBox.SelectBoxStyle.background.,所以它应该自动发生。

如果我添加选择框,它不会显示任何内容,并且只有用于 background9Patch 的最小尺寸。文本只有在我手动设置宽度时才可见。

我是否必须以某种方式启用此功能?

我解决了。

添加项目后你必须调用pack()

    SelectBox<String> sb = new SelectBox<>(skin);
    sb.setItems("Hello World", "FooBarBaz", "FizzBuzz");
    sb.pack();
    stage.addActor(sb);