0

我想将“img”元素显示为面板的子元素。我们应该为此使用 Image 小部件吗?我正在执行以下操作:

Image img = new Image();
img.getElement().getStyle().setWidth(80, Style.Unit.PX);
img.getElement().getStyle().setHeight(80, Style.Unit.PX);

// add it to a panel, which eventually gets added to the root panel.
somePanel.add(img);

// set the url
img.setUrl("stuff/test.png");

这在 FF 上运行良好。当我在移动 Safari 中运行相同的代码时,图像永远不会加载。我已经向 Image 实例添加了一个 LoadHandler。我可以看到在 FF 上触发了事件回调。但在移动 safari 上,它们永远不会被触发。

我记得必须将图像添加到 RootPanel 才能加载它,但我确实这样做了。还有什么可能导致这种情况?我确定 png 资源的位置正确。

谢谢

4

1 回答 1

0

经验法则:始终使用绝对 URL,可能使用GWT.getModuleBaseURL(),GWT.getHostPageBaseURL()GWT.getModuleBaseForStaticFiles()作为前缀:

img.setUrl(GWT.getHostPageBaseURL() + "stuff/test.png");

这是因为 GWT 代码在 iframe 中运行并从子文件夹中加载,并且浏览器在使用主机页面还是 iframe URL 来解析相对 URL 的行为上有所不同。

于 2013-01-03T10:15:58.310 回答