要在Android WebView中禁用缩放,您需要重写WebView的onTouchEvent
方法并处理缩放事件。以下是一个示例代码:
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.webkit.WebView; import android.webkit.WebViewClient; public class NoZoomWebView extends WebView { public NoZoomWebView(Context context) { super(context); init(); } public NoZoomWebView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public NoZoomWebView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { setWebViewClient(new WebViewClient()); setOverScrollMode(OVER_SCROLL_NEVER); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { this.setHorizontalScrollBarEnabled(false); this.setVerticalScrollBarEnabled(false); } if (event.getPointerCount() > 1) { return true; } switch (event.getAction()) { case MotionEvent.ACTION_MOVE: return true; case MotionEvent.ACTION_UP: this.setHorizontalScrollBarEnabled(true); this.setVerticalScrollBarEnabled(true); break; } return super.onTouchEvent(event); } }
然后,在布局文件中使用这个自定义的WebView:
这样,您的WebView将禁用缩放功能。