16

WebView で垂直スクロールのみを使用し、水平スクロールは必要ありません。

webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

これは、スクロールの問題を解決するのに役立ちました。

しかし、これを使用すると、私のウェブビューが奇妙に見えました。すべての編集テキストの高さが (垂直方向に) 圧縮され、見栄えが悪くなります。

  • クライアント側から水平スクロールを無効にする他の方法はありますか?

  • SINGLE_COLUMN を使用して、webview の高さの変更に関する問題を回避するにはどうすればよいですか? 垂直方向のルック アンド フィールを、SINGLE_COLUMN を使用しない場合と同じにしたい

4

8 に答える 8

34

これは、webview に対してのみ水平スクロールを無効にする方法です。

webView.setHorizontalScrollBarEnabled(false);
webView.setOnTouchListener(new View.OnTouchListener() {
    float m_downX;
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getPointerCount() > 1) {
            //Multi touch detected
            return true;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                // save the x
                m_downX = event.getX();
                break;
            }
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP: {
                // set x so that it doesn't move
                event.setLocation(m_downX, event.getY());
                break;
            }

        }
        return false;
    }
});

基本的に、タッチ イベントをインターセプトし、x 値の変更を許可しません。これにより、webview を垂直方向にスクロールできますが、水平方向にはスクロールできません。反対が必要な場合は、y に対して実行します。

于 2012-09-18T21:07:55.533 に答える
29

これはハックですが、過去にうまくいったものです。WebViewを垂直方向のScrollViewで囲みます。

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"    >
  <WebView
    android:id="@+id/mywebview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</ScrollView>

次に、WebViewのすべてのスクロールを無効にします。

WebViewのスクロールを無効にするには、次のコードを使用できます。

 // disable scroll on touch
webview.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
      return (event.getAction() == MotionEvent.ACTION_MOVE);
    }
});
于 2012-06-16T14:06:04.827 に答える
3

私はちょうどこれを試してみましたが、それは魔法のように機能しました.1行だけのこのシンプルなものだとは知りませんでした:

mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

ソース: http://sharecoding.wordpress.com/2012/02/16/programmatically-disable-scroll-function-on-android-webview/

于 2013-11-14T10:05:49.503 に答える
0

veeによる回答に続いて、ここに垂直のみの WebView クラスがあります。これを xml 内のビュー要素として使用して、コードをきれいに保つことができます。

package com.yourpackage.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;

/**
 * custom {@link WebView} which only scrolls vertically but not horizontally
 */
public class VerticalScrollWebview extends WebView implements View.OnTouchListener {

    // the initial touch points x coordinate
    private float touchDownX;

    public VerticalScrollWebview(Context context) {
        super(context);

        // make vertically scrollable only
        makeVerticalScrollOnly();
    }

    public VerticalScrollWebview(Context context, AttributeSet attrs) {
        super(context, attrs);

        // make vertically scrollable only
        makeVerticalScrollOnly();
    }

    public VerticalScrollWebview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // make vertically scrollable only
        makeVerticalScrollOnly();
    }

    /**
     * make this {@link WebView} vertically scroll only
     */
    private void makeVerticalScrollOnly() {
        // set onTouchListener for vertical scroll only
        setOnTouchListener(this);

        // hide horizontal scroll bar
        setHorizontalScrollBarEnabled(false);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        // multitouch is ignored
        if (motionEvent.getPointerCount() > 1) {
            return true;
        }

        // handle x coordinate on single touch events
        switch (motionEvent.getAction()) {
            // save the x coordinate on touch down
            case MotionEvent.ACTION_DOWN:
                touchDownX = motionEvent.getX();
                break;

            // reset the x coordinate on each other touch action, so it doesn't move
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                motionEvent.setLocation(touchDownX, motionEvent.getY());
                break;

        }

        // let the super view handle the update
        return false;

    }
}
于 2016-10-31T10:41:33.320 に答える