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.
一个简单的例子:
A Quick example:
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大小设置子位置和size:
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:
- 检索尚未显示的小部件的大小
- 当小部件显示时通知
- retrieve the size of a widget that has not been displayed
- be notified when a widget is displayed
推荐答案
我认为你应该尝试用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屋!