WebView 的scrollTo 不起作用
我正在尝试使用 webview 的scrollTo 方法。这是我的网络视图布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
我想做的是显示一个 html 文件(只有一个地图图像)并滚动到图像上的特定区域:
mapWebView.loadUrl("file:///android_asset/maps/map.html");
mapWebView.scrollTo(300, 300);
但是当加载 webview 时,它总是显示图像的 (0, 0) 。
这里的 scrollTo()
有什么问题?
I'm trying to use scrollTo method of webview. This my layout file for the webview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
What I'm trying to do is showing a html file (which has only a map image) and scrolling to certain area on image with :
mapWebView.loadUrl("file:///android_asset/maps/map.html");
mapWebView.scrollTo(300, 300);
But when the webview is loaded it always shows the (0, 0) of the image.
What is the problem with scrollTo()
here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑
mapWebView.loadUrl("file:///android_asset/maps/map.html");
是异步的,所以mapWebView.scrollTo(300, 300);
在 webview 完成加载之前执行。页面加载后,它将丢失您应用的滚动设置,并将重置到顶部。您需要监听页面加载,然后滚动它:
希望这有帮助
编辑:事实证明这是不可靠的,请改用它:
I suspect that
mapWebView.loadUrl("file:///android_asset/maps/map.html");
is asynchronous, somapWebView.scrollTo(300, 300);
executes before the webview has finished loading. Once the page loads it will have lost the scroll setting you applied and will be reset to the top.You need to listen in for the the page loading and then scroll it:
Hope this helps
EDIT: Turns out this is unreliable, use this instead:
试试这个...:
Try this..:
遵循覆盖对我来说效果更好。
Following override works better for me.
这有点笨拙,但确实有效。在
onPageFinished
中使用带有 DOM 的 javascript 来描述您要从哪里开始。我最初实现它是为了在页面加载后移动到指定的锚点。如果没有锚点,我需要它转到页面顶部,所以我只需查找“html
”标签。This is a little kludgy, but it works. Use javascript with DOM in
onPageFinished
to describe where you want to start. I originally implemented it to move to a specified anchor after the page loaded. If there is no anchor, I need it to go to the top of the page, so I just look for the 'html
' tag.