如果我使用以下代码使用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);
}
Run Code Online (Sandbox Code Playgroud)
我添加的标签horizontalLayout未显示在创建的UI中.这是为什么?在这种情况下,我期望发生的是标签采取所需的宽度,按钮采取宽度的其余部分.但标签根本没有显示.
请不要问我为什么要扩展按钮.这只是一个MCVE,我真正的UI有点复杂.
该Button默认都是不确定的大小.所以空间将被共享100%尺寸Label(默认),用0扩大比和过量空间的的Button细胞与扩大比例1.因此,所有的空间给该按钮的多余空间.
设置Label为具有未定义的大小,label.setSizeUndefined() 并将按预期工作.
请注意,使用扩展比率时,相对大小的组件可能会丢失所有空间.
例如:
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);
Run Code Online (Sandbox Code Playgroud)
将只显示标签"两个".
| 归档时间: |
|
| 查看次数: |
988 次 |
| 最近记录: |