Vaadin Web应用开发教程(29):UI布局-VerticalLayout和HorizontalLayout布局

jerry VaadinWeb 2015年11月25日 收藏

VerticalLayout和HorizontalLayout 分别垂直和水平安排其中的UI组件。这是Vaadin框架中两个最为重要的布局方式。比如Window及其父类Panel 缺省的布局就为VerticalLayout。
这两种布局的基本用法如下:

  1. VerticalLayout vertical = new VerticalLayout ();
  2. vertical.addComponent(new TextField("Name"));
  3. vertical.addComponent(new TextField("Street address"));
  4. vertical.addComponent(new TextField("Postal code"));
  5. main.addComponent(vertical);


改成使用HorizontalLayout ,显示如下:

此外可以通过setSpacing() 来修改UI组件之间的空隙,setComponnetAlignment 来修改UI组件的对齐方式。
要注意的是,布局中UI组件的实际占据的大小和位置和UI组件自身宽度和长度设置的不同有所不同。

比如对于VerticalLayout来说,如果其高度使用Sizeable.SIZE_UNDEFINED 将其设为“未定义”,则其高度自适应其包含的UI组件的高度,同理HorizontalLayout 布局的宽度为“未定义”HorizontalLayout 宽度也取决于其所包含的其她UI组件。

注: 如果布局包含的UI组件使用了“百分比”来定义高度或宽度,则要求Layout必须定义对应的宽度或高度。但如果通过其中包含的某些UI组件可以确定布局的宽度或高度,在这种情况下可以不定义布局类的大小
比如:

  1. // Vertical layout would normally have 100% width
  2. VerticalLayout vertical = new VerticalLayout();
  3.  
  4. // Shrink to fit the width of contained components
  5. vertical.setWidth(Sizeable.SIZE_UNDEFINED, 0);
  6.  
  7. // Label has normally 100% width, but we set it as
  8. // undefined so that it will take only the needed space
  9. Label label =
  10. new Label("\u2190 The VerticalLayout shrinks to fit "+
  11. "the width of this Label \u2192");
  12. label.setWidth(Sizeable.SIZE_UNDEFINED, 0);
  13. vertical.addComponent(label);
  14.  
  15. // Button has undefined width by default
  16. Button butt = new Button("\u2190 This Button takes 100% "+
  17. "of the width \u2192");
  18. butt.setWidth("100%");
  19. vertical.addComponent(butt);

这个例子使用VerticalLayout 布局,将其宽度设为“未定义”,而Button的宽度设为“100%”,此时如果没有Label组件,则必须为这个VerticalLayout 指定宽度,否则无法知道这个“100%”是哪个的“100%”。Label组件的宽度为“未定义”,其宽度取决于其显示字符的长度,在这个例子中,Button的宽度设为“100%”,因此与Label等宽。

在指定指定布局大小的情况下,缺省情况是将其包含的UI组件均匀间隔排列。
例如使用HorizontalLayout,并指定其宽度为400px。

  1. HorizontalLayout fittingLayout = new HorizontalLayout();
  2. fittingLayout.setWidth("400px");
  3. fittingLayout.addComponent(new Button("Small"));
  4. fittingLayout.addComponent(new Button("Medium-sized"));
  5. fittingLayout.addComponent(new Button("Quite a big component"));
  6. mainWindow.addComponent(fittingLayout);


有些情况下,你可以希望其中某个UI组件占据所有剩余空间,可以为UI组件设置扩展比例(类似于Android 中权重)。扩展比例由方法Layout对象setExpandRatio()指定,第二个参数为扩展的权重。
比如修改上面代码,不均匀安排三个按钮,而是让阿第三个按钮占据所剩余的空间。

  1. HorizontalLayout fittingLayout = new HorizontalLayout();
  2. fittingLayout.setWidth("400px");
  3. fittingLayout.addComponent(new Button("Small"));
  4. fittingLayout.addComponent(new Button("Medium-sized"));
  5. // This button will expand.
  6. Button expandButton = new Button("Expanding component");
  7. // Use 100% of the expansion cell's width.
  8. expandButton.setWidth("100%");
  9. // The component must be added to layout before setting the ratio.
  10. fittingLayout.addComponent(expandButton);
  11. // Set the component's cell to expand.
  12. fittingLayout.setExpandRatio(expandButton, 1.0f);
  13. mainWindow.addComponent(fittingLayout);


然而,如果Layout所包含的UI组件没有定义大小,(如没有使用如setWidth(100%))时,扩展比例则是应用到UI组件之间的空间。

  1. HorizontalLayout layout = new HorizontalLayout();
  2. layout.setWidth("400px");
  3.  
  4. // Create three equally expanding components.
  5. String[] captions = { "Small", "Medium-sized",
  6. "Quite a big component" };
  7. for (int i = 1; i <= 3; i++) {
  8. Button button = new Button(captions[i-1]);
  9. layout.addComponent(button);
  10.  
  11. // Expand ratios are 1:2:3.
  12. layout.setExpandRatio(button, i * 1.0f);
  13. }