我在GWT中编写了自己的PopupPanel
。我想显示相对于其他小部件的弹出窗口。我对该类的实现如下所示:
public class Popover extends PopupPanel implements PositionCallback {
private static final Binder binder = GWT.create(Binder.class);
private UIObject relative;
interface Binder extends UiBinder<Widget, Popover> {
}
public Popover() {
setWidget(binder.createAndBindUi(this));
setStylePrimaryName("popover");
}
public void show(UIObject relative) {
this.relative = relative;
setPopupPositionAndShow(this);
}
public void setPosition(int offsetWidth, int offsetHeight) {
if (relative != null) {
int left = relative.getAbsoluteLeft();
int top = relative.getAbsoluteTop();
int width = relative.getOffsetWidth();
int height = relative.getOffsetHeight();
int topCenter = top + height / 2 - offsetHeight / 2;
if (offsetWidth < left) {
setPopupPosition(left - offsetWidth, topCenter);
} else {
setPopupPosition(left + width, topCenter);
}
}
}
}
问题是offsetWidth
和offsetHeight
始终是10
?
我的Popover.ui.xml
如下所示:
<g:FlowPanel stylePrimaryName="popover">
<g:FlowPanel stylePrimaryName="arrow" />
<g:FlowPanel stylePrimaryName="inner">
<g:Label stylePrimaryName="title" text="New Label" />
<g:FlowPanel stylePrimaryName="content">
<g:Label text="Hallo" />
</g:FlowPanel>
</g:FlowPanel>
</g:FlowPanel>
答案 0 :(得分:5)
要解决此问题,我按照PopupPanel
的{{1}}使用的步骤进行了操作,但我将定位步骤作为延迟命令。我的代码没有扩展setPopupPositionAndShow()
,因此PopupPanel
变量:
popupPanel
答案 1 :(得分:1)
getOffsetWidth()
和getOffsetHeight()
仅在DOM完全构造且您想要获取该值的容器可见且已附加时才起作用。
为什么不使用showRelativeTo()
的<{1}}功能?
PopupPanel