能否利用CssLayout实现组件的绝对定位?

Can CssLayout be used to achieve absolute positioning of components?

目前我正在使用AbsoluteLayout,但由于一些问题我想试试CssLayout

我想在布局中的特定位置动态放置图像、标签和按钮组件。

AbsoluteLayout 允许我像这样指定位置:

absoluteLayout.addComponent(component, "top:20px;left:20px")

用 CssLayout 完全可以实现这样的功能吗?

您可以通过重写 CssLayout 上的 getCss 为组件应用内联 CSS,因此下面应该有想要的结果:

  CssLayout cssLayout = new CssLayout() {
    @Override
    protected String getCss(Component component) {
        // check the component here and return correct css. In this case only one component in the layout so this works..
        return "position: relative; top: 10px; left: 10px";
    }  
  };
  cssLayout.setSizeFull();

  cssLayout.addComponent(new Button("Hello"));