2012-01-11 40 views
3

私はレイアウトを持っており、WebViewでHTMLドキュメントをレンダリングしています。WebViewでAndroidをスクロールするにはどうすればいいですか

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コードは、私がスクロールしたりしても、私はスクロールすることはできませんアセットフォルダに保存されているHTMLを呼び出すことはできません呼び出すどんなURL

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); 
     } 

です。 あなたの返事を楽しみにしています。ありがとう。

答えて

5

私はこの問題についてより良い答えを見つけました。 WebViewにはscrollTo(),getScrollX()getScrollY()のメソッドがあります。 Viewから継承されているため(#AbsoluteLayout - >ViewGroup - > View経由)、ドキュメントには少し隠されています。これは明らかに幾分厄介なJavaScriptインターフェイスよりもスクロール位置を操作する良い方法です。

+0

非常に便利なコードの例をありがとうございます。 – Mukunda

+0

まず、何かを学んだらコードを書くのは時間の無駄ではありません。 :-) あなたの情報をお寄せいただきありがとうございます。ご質問ありがとうございました –

+0

私はちょうど変更を加えたので、 'scrollTo()'、 'getScrollX()'と 'getScrollY()'を使用する必要はありませんレイアウトでは、WebViewの親がScrollViewの場合、WebViewのスクロールは機能しません。 私はLinearLayoutを使い、LinearLayoutの中でWebViewを呼び出しました。 誰かがScrollViewを親レイアウトとして使用したい場合、ScrollViewではLinearLayoutを使用し、LinearLayoutではWebViewを使用すると完全に動作します。 – Mukunda

1

はまた、tryブロックの前にfindViewByIdを行う

wv.loadData(text, mimeType, encoding); 

wv.setWebChromeClient(new WebChromeClient()); 

を試してみてください。

+0

私はすでに試してみましたが動作しませんでした.ScrollViewの内部にある場合はScroll for WebViewは機能しませんが、ScrollViewを親レイアウトとして配置してからScrollView内のLinearlayoutを取り、LinearLayout内部でWebViewを宣言しています。 – Mukunda

関連する問題