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

我想只有在我的webview垂直滚动,不希望任何水平滚动。

webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

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

但使用这使得我的webview看起来很奇怪。 所有的编辑文本的高度挤压(垂直),看起来不好。

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

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

Solutions Collecting From Web of "如何在Android webview中禁用水平滚动"

这是一个黑客攻击,但过去一个成功的工作。 在一个垂直方向的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); } }); 

这里是我如何禁用水平滚动只为一个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可以垂直滚动,但不能水平滚动。 如果你想要相反的话,那就去做吧。

我只是试了一下,就像一个魅力,我不知道这是简单的,只有一行:

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

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

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

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

 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; } } 
  webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING); 

橱窗 在Android 7.1中