GWT LayoutPanel 中的中间居中对齐(Alignment.MIDDLE_CENTER 等效)
如何将LayoutPanel放置在父LayoutPanel(RootLayoutPanel、屏幕、浏览器页面)的中间中心?假设内部面板具有固定大小,则外部面板是灵活的并且可以调整大小(随着浏览器窗口)。
在 Vaadin 中,这是一行命令:(VerticalLayout)outerPane).setComponentAlignment(innerPane, Alignment.MIDDLE_CENTER);
GWT 等效项是什么?
GWT Layout.Alignment 枚举仅提供 BEGIN END STRETCH。
不足。屏幕居中放置对于显示较小的表单、对话框、警告消息、登录屏幕等至关重要。
详细信息请参阅 我在 GWT 论坛上的帖子
谢谢
--- 更新(2012 年 2 月 27 日) ---------------
要使用建议的技术 - 使用 setWidgetLeftRight() 和 setWidgetTopBottom() - 我实际上需要计算 NW 位置 (x ,y) 相对于外部组件的内部组件,并在每次调整外部容器或内部容器的大小(内容高度)后执行此操作。
因此,如果 GWT 不支持居中,并且似乎即使在最新版本中也不支持居中,则涉及更多编码:
outerComponent.setWidgetLeftRight(innerComponent, x, PX, x, PX);
outerComponent.setWidgetTopBottom(innerComponent, y, PX, y, PX);
x,y 计算为:
x = (w1 – w2) / 2
y = (h1 – h2) / 2
其中:
w1 - 外部组件宽度(可变,容易获得?)
h1 - 外部组件高度(可变,容易获得?)
w2 - 内部居中组件宽度(固定)
h2 - 内部居中组件高度(可变,取决于内容,可能很难获得)
最好全部以像素(PX?)或 EM 为单位。当与 % 混合时,必须发生更多的转换。
每次发生调整大小事件时,都必须重新计算坐标(w1、h1 更改)。
随着内部内容的每次更改,都必须重新计算坐标(h2 更改)。
它几乎是变相的绝对布局(AbsolutePanel)。
CSS 解决方案?我在此服务器上找到了几个示例,但没有一个标记为已解决。没有一个同时具有垂直和水平居中以及灵活的内部组件高度。
为什么我不喜欢基于 CSS 的布局解决方案:
- 容易出现跨浏览器问题。 GWT 在这里帮不了你。
- 我认为在代码和 CSS 中混合布局是一个糟糕的设计决策。 CSS 最好留给“装饰”声明(字体大小和颜色、边距、填充)。
- 我自己在 CSS 布局方面的糟糕经历;容易损坏,难以调试,在将所有东西放在一起之前会遇到很多令人头痛的事情。最好避免。我对 GWT 抱有很大的希望。
我正在考虑这个基于代码的解决方案:
- Widget 的新子类?控制板?布局?成分? (内部组件)
- 实现 RequiresResize 期望,实现 onResize() 回调方法
- 获取变量尺寸(w1、h1、h2)的实际值。如何?
- 为内部组件设置新坐标。如何?
指向基于这些前提提供解决方案的任何人:)
How to place LayoutPanel in the middle center of parent LayoutPanel (RootLayoutPanel, screen, browser page)? Assuming the inner panel is of fixed size, outer panel is flexible and does resize (with the browser window).
In Vaadin it is one line command:(VerticalLayout)outerPane).setComponentAlignment(innerPane,
Alignment.MIDDLE_CENTER);
What is the GWT equivalent?
The GWT Layout.Alignment enum provides only BEGIN END STRETCH.
Inadequate. Screen centered placement is essential for displaying smaller forms, dialog boxes, warning messages, login screens etc.
Details on my post on the GWT forum
Thank you
--- UPDATE (27.2.2012) ---------------
To use suggested technique - using setWidgetLeftRight() and setWidgetTopBottom() - I need to compute in fact NW position (x,y) of inner component relative to outer component and do it after every resize of outer container or inner container (content height).
So if GWT does not support centering, and it seems that even in the latest version it does not, there is more coding involved:
outerComponent.setWidgetLeftRight(innerComponent, x, PX, x, PX);
outerComponent.setWidgetTopBottom(innerComponent, y, PX, y, PX);
x,y to be computed as:
x = (w1 – w2) / 2
y = (h1 – h2) / 2
where:
w1 - outer component width (variable, easy to obtain?)
h1 - outer component height (variable, easy to obtain?)
w2 - inner centered component width (fixed)
h2 - inner centered component height (variable, depending on content, potentially tricky to obtain)
All preferably in pixels (PX?) or EM. When mixed with %, more conversion would have to take place.
With every resize event happens the coordinates has to be recalculated (w1, h1 change).
With every change of the internal content the coordinates has to be recalculated (h2 changes).
It is pretty much absolute layout (AbsolutePanel) in disguise.
CSS solution? I found several examples on this server, but none marked as solved. None with both vertical and horizontal centering and flexible inner component height.
Why I don't prefer CSS based layout solution:
- Prone to cross browser issues. GWT doesn't help you here.
- I consider mixing layout in code and in CSS a bad design decision. CSS is best to be left for “decorative” declarations (font sizes and colors, margins, paddings).
- My own bad experience with CSS layouts; easy to break, difficult to debug, lot's of headaches before you put everything together. Best to avoid. I had great hopes towards GWT.
I am thinking about this code based solution:
- new subclass of Widget? Panel? Layout? Component? (the inner component)
- implement RequiresResize expect, implement onResize() callback method
- obtain actual values for variable dimensions (w1, h1, h2). How?
- set new coordinates for the inner component. How?
Points to anyone providing solution based on these premises :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您可以使用
VerticalPanel
并设置 HorizontalAlignment 字段或者如果您想使用LayoutPanels
您可以调整您在 google groups thread 中发布的 GWT 文档中的解决方案。这将创建一个中心面板,左、右、上、下边距为 5 em,如下所示(只有中间的白色面板可见)。 EM 是类似于像素的固定大小,但取决于基本字体大小。如果您的基本字体大小(为您的正文定义的字体大小)设置为 16 px,则
1 EM = 16 px
(请参阅 此处了解转化信息)。您还可以使用边距的相对值:
但是,这样您将无法完全解决您的问题(不同的表单尺寸 -> 请参阅 google thread 中的屏幕截图)因为中心面板始终具有固定的边距(5 EM 或 5 %),与中心面板的垂直尺寸无关。
此外,如果中心面板的内容高于可用的屏幕尺寸,则中心面板的内容将被剪裁,并且当您调整浏览器大小时,中心面板的内容也会调整大小。
所以我认为更好的方法是将
FlowPanel
/HTMLPanel
放入 LayoutPanel 中并应用 CSS 样式以允许自然调整大小。Well you could use a
VerticalPanel
and set the HorizontalAlignment field or in case you want to useLayoutPanels
you can adapt the solution in the GWT docs which you posted in the google groups thread.This will create a a center panel with 5 em margins left, right, top and bottom like below (only the white panel in the middle will be visible). EM is a fixed size similar to pixel but depends on the base font size. If your base font size (the one defined for your body) is set to 16 px then
1 EM = 16 px
(see here for conversion infos).You could also use relative values for the margins:
However this way you wont be able to solve your problem entirely (different form sizes -> see screenshots in the google thread) because the center panel will always have fixed margins (5 EM or 5 %) independent of the vertical size of the center panel.
Furthermore the center panel's content will be clipped if it is taller than the available screen size and will be resized when you resize your browser.
So I think the better approach is to put a
FlowPanel
/HTMLPanel
inside a LayoutPanel and apply CSS styles to allow natural resizing.如果您将 ResizeListener 添加到中心面板会怎么样(我不确定这是否有效,现在就尝试一下)。基本上,每当窗口调整大小时,面板都会自行居中(通过计算其相对于其父级高度的高度)。
编辑
我最终通过让我的面板实现 ResizeHandler 并具有以下内容得到了我想要的东西:
What if you added a ResizeListener to your center panel (I'm not sure this works, will try it now). Basically, whenever the window resizes, the panel centers itself (by calculating it's height relative to the height of its parent).
EDIT
I ended up getting what I wanted by letting my panel implement ResizeHandler and having the following: