如果其他布局组件设置为展开,Vaadin 标签将隐藏

Vaadin label gets hidden if other layout component is set to expand

如果我使用以下代码使用 Vaadin 创建 UI,

@Override
protected void init(VaadinRequest request) {
    HorizontalLayout horizontalLayout = new HorizontalLayout();

    horizontalLayout.addComponent(new Label("Why am I not shown?"));
    Button button = new Button("expanding button");
    horizontalLayout.addComponent(button);
    horizontalLayout.setExpandRatio(button, 1);

    horizontalLayout.setWidth(100, Unit.PERCENTAGE);
    setContent(horizontalLayout);
}

我添加到 horizontalLayout 的标签在创建的 UI 中没有显示。这是为什么?在这种情况下,我希望发生的是标签采用所需的宽度,而按钮采用其余的宽度。但是标签根本没有显示。

请不要问我为什么要展开按钮。这只是一个 MCVE,我的真实 UI 稍微复杂一些。

Button 默认大小未定义。因此 space 将由 100% 大小的 Label(默认情况下)共享,扩展比率为 0, 多余的 space Button 单元格的展开比率为 1。所以所有 space 都给了按钮的多余 space。

Label 设置为具有 label.setSizeUndefined() 的未定义大小,并将按您预期的方式工作。

请注意,使用扩展比例时,相对大小的组件可能会丢失所有 space。

例如:

 HorizontalLayout horizontalLayout = new HorizontalLayout();
 Label l1 = new Label("one");
 Label l2 = new Label("two");
 Label l3 = new Label("three");
 horizontalLayout.addComponent(l1);
 horizontalLayout.addComponent(l2);
 horizontalLayout.addComponent(l3);
 horizontalLayout.setExpandRatio(l2, 1);

将仅显示标签 "two"。