Vaadin Web应用开发教程(36):UI布局-AbsoluteLayout 布局

jerry VaadinWeb 2015年11月25日 收藏

AbsoluteLayout 布局允许将其中的UI组件任意指定其位置。方法addComponent()可以指定相对于AbsoluteLayout边界的纵横坐标。还可以指定一个Z方向深度值,用来表示UI组件的前后顺序。
UI组件的位置是通过left,right,top,bottom 及z-index 来指定(CSS对应的属性)。

  1. // A 400x250 pixels size layout
  2. AbsoluteLayout layout = new AbsoluteLayout();
  3. layout.setWidth("400px");
  4. layout.setHeight("250px");
  5. // A component with coordinates for its top-left corner
  6. TextField text = new TextField("Somewhere someplace");
  7. layout.addComponent(text, "left: 50px; top: 50px;");
  8.  
  9. // At the top-left corner
  10. Button button = new Button( "left: 0px; top: 0px;");
  11. layout.addComponent(button, "left: 0px; top: 0px;");
  12. // At the bottom-right corner
  13. Button buttCorner = new Button( "right: 0px; bottom: 0px;");
  14. layout.addComponent(buttCorner, "right: 0px; bottom: 0px;");
  15. // Relative to the bottom-right corner
  16. Button buttBrRelative = new Button( "right: 50px; bottom: 50px;");
  17. layout.addComponent(buttBrRelative, "right: 50px; bottom: 50px;");
  18. // On the bottom, relative to the left side
  19. Button buttBottom = new Button( "left: 50px; bottom: 0px;");
  20. layout.addComponent(buttBottom, "left: 50px; bottom: 0px;");
  21. // On the right side, up from the bottom
  22. Button buttRight = new Button( "right: 0px; bottom: 100px;");
  23. layout.addComponent(buttRight, "right: 0px; bottom: 100px;");


除了使用px 指定据边界的距离外,也可以使用百分比。

  1. // A panel that takes 30% to 90% horizontally and
  2. // 20% to 80% vertically
  3. Panel panel = new Panel("A Panel");
  4. panel.setSizeFull(); // Fill the specified area
  5. layout.addComponent(panel, "left: 30%; right: 10%;" +
  6. "top: 20%; bottom: 20%;");