我需要设置absolutePanel的大小与其子大小有关,但getOffset*方法返回0,因为(我认为)孩子还没有显示.
一个简单的例子:
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
Run Code Online (Sandbox Code Playgroud)
我还可以通过使用AbsolutePanel大小来设置子位置和大小来解决我的问题:
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);
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我必须找到一种方法:
我认为你应该尝试用 css 样式来解决这个问题。如果我是您,我会尝试使用 addStyle 或 addStyleNames 为父面板和/或绝对面板设置样式。也许像...
AbsolutePanel aPanel = new AbsolutePanel();
aPanel.setSize("100%", "50px");
HTML text = new HTML(/*variable lenght text*/);
text.addStyleName("my-custom-style");
Run Code Online (Sandbox Code Playgroud)
在 css 中,您将创建一个 css 样式,然后它会适当地调整您的小部件的大小。
另一种方法是......
您可以创建一个函数来创建完全符合您需要的大小的 HTML 小部件。
这里有一些代码可以帮助您入门:
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");
...
}
Run Code Online (Sandbox Code Playgroud)
有关 JSNI 的一些信息 - http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html