的WebView scrollTo不工作 [英] WebView scrollTo is not working
问题描述
我想要使用的WebView的scrollTo方法。这是我的布局文件的web视图。
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>
我试图做的是呈现一个HTML文件(其中只有一个地图图像),并滚动到特定区域的图像:
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);
但是,当web视图被加载它总是显示图像的(0,0)。
But when the webview is loaded it always shows the (0, 0) of the image.
有什么问题与 scrollTo()
吗?
推荐答案
我怀疑 mapWebView.loadUrl(文件:///android_asset/maps/map.html);
是异步的,所以 mapWebView.scrollTo(300,300);
执行前的WebView加载完毕。当网页加载它会失去滚动设置,应用和将被重置到顶部。
I suspect that mapWebView.loadUrl("file:///android_asset/maps/map.html");
is asynchronous, so mapWebView.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:
mapWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mapWebView.scrollTo(300, 300);
}
}
);
希望这有助于
Hope this helps
编辑:原来,这是不可靠的,用这个来代替:
Turns out this is unreliable, use this instead:
mapWebView.setPictureListener(new PictureListener() {
@Override
public void onNewPicture(WebView view, Picture picture) {
mapWebView.scrollTo(300, 300);
}
});
这篇关于的WebView scrollTo不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!