【问题标题】:How to disable horizontal scrolling in Android webview如何在 Android webview 中禁用水平滚动
【发布时间】:2012-06-16 13:52:44
【问题描述】:

我只想在我的 web 视图中进行垂直滚动,不希望任何水平滚动。

webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

这帮助我解决了滚动问题。

但是使用它让我的 webview 看起来很奇怪。所有编辑文本的高度都被挤压(垂直)并且看起来很糟糕。

  • 还有其他方法可以禁用客户端的水平滚动吗?

  • 使用 SINGLE_COLUMN 我们如何避免 webview 高度变化的问题?我希望垂直外观与没有 SINGLE_COLUMN 时的外观相同

【问题讨论】:

标签: java android webview


【解决方案1】:

以下是我只为 web 视图禁用水平滚动的方法。

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 做。

【讨论】:

  • 但它禁用了 Webview 中的单击操作。有什么帮助吗?
  • @vee :无法解析符号 m_downX 。 m_downX 是什么,我的 android studio 无法识别
  • @Amir.KH 它的变量名。只需添加它
  • 这也会为我禁用在 web 视图中的点击。
  • @TomKincaid 它不会禁用点击,这是可行的
【解决方案2】:

这是一种 hack,但过去曾成功地为我工作过。在垂直方向的 ScrollView 中环绕您的 WebView:

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

【讨论】:

  • 如果我没记错,它将禁用水平和垂直滚动
  • 不 - 垂直 ScrollView 将允许滚动 WebView 的垂直内容。
  • @SBerg:感谢您的建议。这个解决方案运行良好:).. 我只是想知道这是否是阻止水平滚动的唯一方法。
  • 但是当我使用 loadDataWithBaseUrl 加载 html 文本时,它会水平切断内容。有什么解决办法吗?
  • 实现上述 setOnTouchListener 将禁用双向滚动!
【解决方案3】:

我刚试过,效果很好,没想到这么简单,就一行:

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

来源:http://sharecoding.wordpress.com/2012/02/16/programmatically-disable-scroll-function-on-android-webview/

【讨论】:

【解决方案4】:

基于@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 一起使用:

 webView.setHorizontalScrollBarEnabled(false);
 webView.setOnTouchListener(new WebViewTouchListener());

【讨论】:

    【解决方案5】:

    按照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;
    
        }
    }
    

    【讨论】:

      【解决方案6】:

      我知道它很晚,但我希望它对某人有所帮助,请使用下面提到的类来禁用水平滚动。

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

      【讨论】:

        【解决方案7】:
         webView.setHorizontalScrollBarEnabled(false);
        

        定义是否绘制水平滚动条。默认不绘制滚动条。

        【讨论】:

          【解决方案8】:
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
          

          Android 7.1 中的

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-08-15
            • 2019-04-20
            • 2015-02-15
            • 1970-01-01
            • 2015-03-26
            • 1970-01-01
            • 2020-01-23
            • 1970-01-01
            相关资源
            最近更新 更多