在滚动使用的Android的WebView可以正常使用 [英] Scrolling in Android with WebView Working Perfectly
问题描述
我有一个版面,我通过的WebView呈现HTML文档中了。
I have an layout and I am rendering HTML document in it through WebView.
XML布局
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/header"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@drawable/black"
android:tileMode="repeat">
<ImageButton
android:id="@+id/btnBackHelp"
android:src="@drawable/greenarrow"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/black"
android:tileMode="repeat"/>
<ImageView
android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="30dip">
<WebView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/helpBrowserWebview"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:layout_weight="1"
android:weightSum="5"
android:orientation="horizontal"
android:background="@drawable/black"
android:tileMode="repeat">
<LinearLayout
android:id="@+id/footerLayoutHome"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/footerMainBtnHome"
android:layout_width="fill_parent"
android:layout_height="14dip"
android:src="@drawable/home"
android:background="@drawable/black"/>
<TextView
android:text="Home"
android:textSize="8dip"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/footerLayoutProducts"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/footerMainBtnProducts"
android:layout_width="fill_parent"
android:layout_height="14dip"
android:src="@drawable/products"
android:background="@drawable/black"/>
<TextView
android:text="Products"
android:textSize="8dip"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/footerLayoutCart"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/footerMainBtnCart"
android:layout_width="fill_parent"
android:layout_height="14dip"
android:src="@drawable/cart"
android:background="@drawable/black"/>
<TextView
android:text="Cart"
android:textSize="8dip"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/footerLayoutFeedback"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/footerMainBtnFeedback"
android:layout_width="fill_parent"
android:layout_height="14dip"
android:src="@drawable/feedback"
android:background="@drawable/black"/>
<TextView
android:text="Feedback"
android:textSize="8dip"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/footerLayoutHelp"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/footerMainBtnHelp"
android:layout_width="fill_parent"
android:layout_height="14dip"
android:src="@drawable/help"
android:background="@drawable/black"/>
<TextView
android:text="Help"
android:textSize="8dip"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Java的code是
java code is
try {
InputStream is = getAssets().open("help.html");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a Java string.
String text = new String(buffer);
final String mimeType = "text/html";
final String encoding = "utf-8";
// Finally stick the string into the web view.
WebView wv = (WebView)findViewById(R.id.helpBrowserWebview);
wv.loadData(text, mimeType, encoding);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
什么网址我打电话,我不能滚动,甚至调用存储在资源文件夹,我不能滚动的HTML。
期待你的回复。谢谢。
whatever url I call I cannot scroll or even calling an html stored in asset folder I cannot scroll. Looking forward to your reply. thanks.
推荐答案
我已经找到了更好的回答这个问题。事实证明,WebView中确实有 scrollTo()
, getScrollX()
和 getScrollY()
方法,如你所期望。他们有点隐藏在文档中,因为他们从View继承(通过 AbsoluteLayout
- > 的ViewGroup
- >视图)。这显然是一个更好的方式来操作比有些繁琐的JavaScript接口的WebView
滚动位置。
I've found a better answer to this issue. It turns out that WebView does have scrollTo()
, getScrollX()
and getScrollY()
methods as you'd expect. They're a bit hidden in the documentation because they're inherited from View (via AbsoluteLayout
-> ViewGroup
-> View). This is obviously a better way to manipulate the WebView's
scroll position than the somewhat cumbersome JavaScript interface.
这篇关于在滚动使用的Android的WebView可以正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!