GWT - 检索未显示的小部件的大小 [英] GWT - Retrieve size of a widget that is not displayed
问题描述
我需要根据其子级大小设置 absolutePanel 的大小,但 getOffset* 方法返回 0,因为(我认为)子级尚未显示.
I need to set the size of an absolutePanel regarding to its child size, but the getOffset* methods return 0 because (i think) the child has not been displayed yet.
一个简单的例子:
AbsolutePanel aPanel = new AbsolutePanel();
HTML text = new HTML(/*variable lenght text*/);
int xPosition = 20; // actually variable
aPanel.add(text, xPosition, 0);
aPanel.setSize(xPosition + text .getOffsetWidth() + "px", "50px"); // 20px 50px
我也可以通过使用 AbsolutePanel 大小来设置子位置和大小来解决我的问题:
I could also solve my problem by using the AbsolutePanel size to set the child position and size:
AbsolutePanel aPanel = new AbsolutePanel();
aPanel.setSize("100%", "50px");
HTML text = new HTML(/*variable lenght text*/);
int xPosition = aPanel.getOffsetWidth() / 3; // Once again, getOffsetWidth() returns 0;
aPanel.add(text, xPosition, 0);
在这两种情况下,我都必须找到一种方法:
In both case, i have to find a way to either:
- 检索尚未显示的小部件的大小
- 在显示小部件时收到通知
推荐答案
我认为你应该尝试用 css 样式来解决这个问题.如果我是你,我会尝试使用 addStyle 或 addStyleNames 为父面板和/或绝对面板设置样式.也许像...
I think you should try to solve this with css styles. If I were you, I would try to set a style for parent and/or absolute panel, using addStyle or addStyleNames. Maybe something like...
AbsolutePanel aPanel = new AbsolutePanel();
aPanel.setSize("100%", "50px");
HTML text = new HTML(/*variable lenght text*/);
text.addStyleName("my-custom-style");
在 css 中,您将创建一个 css 样式,然后适当地调整您的小部件的大小.
in css you would create a css style which would then size your widget appropriately.
另一种方法是......
The other approach would be....
您可以创建一个函数来创建一个完全符合您需要的大小的 HTML 小部件.
You could create a function that creates a HTML widget exactly the size you need.
这里有一些代码供您入门:
Here's some code for you, to get you started:
public native void customizeHTMLElement(Element elem, String widthInPx) /*-{
elem.style.width = widthInPx; //this is basically pure js.
}-*/
Then somewhere within your widget make a call to the above function like
public void foo() {
...
customizeHTMLElement(someElement, "20");
...
}
关于 JSNI 的一些信息 - http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html
Some info about JSNI - http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html
这篇关于GWT - 检索未显示的小部件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!