有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android webview中禁用水平滚动

我只想在我的webview中进行垂直滚动,不想进行任何水平滚动

webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

这帮助我解决了滚动问题

但是使用它使我的网络视图看起来很奇怪。所有编辑文本的高度(垂直)被压缩,看起来不好

  • 有没有其他方法可以从客户端禁用水平滚动

  • 使用单_列如何避免webview高度更改的问题?我希望垂直的外观和感觉与以前一样,没有单列


共 (6) 个答案

  1. # 2 楼答案

    我知道它晚了,但我希望它将帮助别人,请使用下面提到的类,以禁用水平滚动

    public class CustomWebView extends WebView implements View.OnTouchListener {
    
        // the initial touch points x coordinate
        private float touchDownX;
        private OnScrollChangedCallback mOnScrollChangedCallback;
    
        public CustomWebView(final Context context) {
            super(context);
    
            // make vertically scrollable only
            makeVerticalScrollOnly();
        }
    
        public CustomWebView(final Context context, final AttributeSet attrs) {
            super(context, attrs);
    
            // make vertically scrollable only
            makeVerticalScrollOnly();
        }
    
        public CustomWebView(final Context context, final AttributeSet attrs, final int defStyle) {
            super(context, attrs, defStyle);
    
            // make vertically scrollable only
            makeVerticalScrollOnly();
        }
    
        @Override
        protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            if (mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
        }
    
        public OnScrollChangedCallback getOnScrollChangedCallback() {
            return mOnScrollChangedCallback;
        }
    
        public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback) {
            mOnScrollChangedCallback = onScrollChangedCallback;
        }
    
        /**
         * Impliment in the activity/fragment/view that you want to listen to the webview
         */
        public static interface OnScrollChangedCallback {
            public void onScroll(int l, int t);
        }
    
        /**
         * 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;
    
        }
    }
    
  2. # 3 楼答案

    这是一次黑客攻击,但在过去对我来说是成功的。在垂直方向的滚动视图中环绕您的网络视图:

    <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);
        }
    });
    
  3. # 4 楼答案

    以下是我如何仅为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做吧

  4. # 5 楼答案

    基于@vee answer的解决方案,但更方便:

    public class WebViewTouchListener implements View.OnTouchListener {
        private float downX;
    
        @Override
        public boolean onTouch(final View v, final MotionEvent event) {
            if (event.getPointerCount() > 1) {
                //multi touch
                return true;
            }
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    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(downX, event.getY());
                    break;
            }
            return false;
        }
    }
    

    只需将其与网络视图一起使用即可:

     webView.setHorizontalScrollBarEnabled(false);
     webView.setOnTouchListener(new WebViewTouchListener());
    
  5. # 6 楼答案

    根据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;
    
        }
    }