我有一个适合整个窗口的ContentPanel.它有一个topComponent,一个中心的小部件和一个bottomComponent.
当ContentPanel被渲染一次后,当我尝试将小部件添加到topComponent时,我遇到了布局问题:
public void onModuleLoad() {
final Viewport viewport = new Viewport();
viewport.setLayout(new FitLayout());
final ContentPanel contentPanel = new ContentPanel(new FitLayout());
contentPanel.setHeaderVisible(false);
final LayoutContainer topContainer = new LayoutContainer(
new FlowLayout());
final Button buttonOne = new Button("Top:One");
topContainer.add(buttonOne);
contentPanel.setTopComponent(topContainer);
contentPanel.add(new Button("Center"));
contentPanel.setBottomComponent(new Button("Bottom"));
viewport.add(contentPanel);
RootPanel.get().add(viewport);
// Later, add a second button to the topComponent ...
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
final Button buttonTwo = new Button("Top:Two");
topContainer.add(buttonTwo); // Doesn't show up at first.
topContainer.layout(); // Now, buttonTwo shows up. But we have
// a new problem: the "Bottom" button disappears...
contentPanel.layout(true); // This doesn't do anything, BTW.
}
});
}
Run Code Online (Sandbox Code Playgroud)
关于这一点的一个有趣的事情是,只要我调整浏览器窗口大小,布局就会自行纠正.我可以做些什么来使其立即正确重新布局(我已尝试layout()在几个地方和组合中添加几个调用等,但到目前为止还没有运气.)
(我正在使用GWT 2.1.1和GXT 2.2.1.)
小智 8
发生这种情况是因为当您向FlowLayout添加另一个组件时,它会调整大小,从而增加其自身的高度,从而将底部组件推到可见区域下方.代码中没有任何东西缩小中心组件,因此底部组件保持在其原始位置.
还有一件事是你使用FitLayout为contentPanel包含3个组件,FitLayout用于容器内部只有一个组件,它应该填充其父组件.
您需要考虑以下事项:
1)使用RowLayout,它可以更好地控制组件的布局方式
2)确定您愿意放置垂直滚动条的组件,因为您要动态添加组件.
对于您当前的要求,以下代码应该足够:
public void onModuleLoad() {
final Viewport viewport = new Viewport();
viewport.setLayout(new FitLayout());
// final ContentPanel contentPanel = new ContentPanel(new FlowLayout());
// contentPanel.setHeaderVisible(false);
final LayoutContainer topContainer = new LayoutContainer(
new FlowLayout());
final Button buttonOne = new Button("Top:One");
topContainer.add(buttonOne);
// contentPanel.setTopComponent(topContainer);
final LayoutContainer centerPanel = new LayoutContainer(new FitLayout());
centerPanel.add(new Button("Center"));
// contentPanel.add(centerPanel);
final LayoutContainer botPanel = new LayoutContainer(new FlowLayout());
botPanel.add(new Button("Bottom"));
// contentPanel.setBottomComponent(botPanel);
final ContentPanel panel = new ContentPanel();
panel.setHeaderVisible(false);
panel.setLayout(new RowLayout(Orientation.VERTICAL));
panel.add(topContainer, new RowData(1, -1, new Margins(4)));
panel.add(centerPanel, new RowData(1, 1, new Margins(0, 4, 0, 4)));
panel.add(botPanel, new RowData(1, -1, new Margins(4)));
viewport.add(panel, new FlowData(10));
RootPanel.get().add(viewport);
// Later, add a second button to the topComponent ...
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
final Button buttonTwo = new Button("Top:Two");
topContainer.add(buttonTwo); // Doesn't show up at first.
panel.layout(true);
}
});
}
Run Code Online (Sandbox Code Playgroud)