使用WebView在Android中滚动完美运行

Muk*_*nda 3 android android-layout

我有一个布局,我通过WebView在其中呈现HTML文档.

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>
Run Code Online (Sandbox Code Playgroud)

java代码是

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);
        }
Run Code Online (Sandbox Code Playgroud)

我调用的任何网址都无法滚动甚至调用存储在资产文件夹中的html我无法滚动.期待你的回复.谢谢.

小智 5

我找到了一个更好的答案来解决这个问题.事实证明,WebView中确实有scrollTo(),getScrollX()而且getScrollY()如你所期望的方法.它们在文档中有点隐藏,因为它们是从View继承的(via AbsoluteLayout- > ViewGroup- > View).这显然是一种WebView's比有点麻烦的JavaScript界面更好的操纵滚动位置的方法.