2013-05-10 72 views
0

我想自动滚动我的WebView,最佳方法是什么?我已经尝试了以下,但似乎没有工作。自动滚动WebView Android

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final WebView wv = (WebView) findViewById(R.id.webView1); 
    wv.loadUrl("www.google.com"); 
    wv.scrollBy(0,2); 
    } 
} 

回答

1

的Android(Java)的基于代码未能达到目的,因此诉诸HTML/Javascript方法(礼貌:http://www.mediacollege.com/internet/javascript/page/scroll.html)。

请在HTML头部标签之间添加以下代码到您的html页面。

<script> 
    function pageScroll() { 
     window.scrollBy(0,50); // horizontal and vertical scroll increments 
     scrolldelay = setTimeout('pageScroll()',500); // scrolls every 500 milliseconds, controls scroll speed 
    } 
    </script> 

而不是一个普通的HTML“体”标签以下内容添加到它

<body onLoad="pageScroll()"> 

我已经测试了桌面版Chrome和Firefox浏览器的代码,它工作正常,但在Android设备上 字符编码失败,从而导致html内容的显示不稳定。

+0

感谢您的回复,但似乎没有向下滚动页面。 – ivesingh 2013-05-10 23:27:11

+0

在这里发布之前,我已经在模拟器以及ACER android平板设备上测试了这段代码。我使用了网址“www.google.com”。请让我知道你在代码中使用的URL/html链接是什么。 – Sriram 2013-05-13 18:00:34

+0

请查看底部的文件名“1.html”,这是我想要在网页加载时自动滚动的文件,并且在html页面到达其末尾时停止。我想scrollBy(0,1)的scrollBy速度。 https://sites.google.com/site/gurbaniujagar/page1 – ivesingh 2013-05-14 15:30:04