经过大量研究,我合并了一些无法单独使用的答案,并意识到它们对我来说非常有效。
这将禁用 WebView 中的双击缩放。
首先,您需要在 WebView 中进行设置:
YourWebView.getSettings().setUseWideViewPort(false);
然后,您需要缩放 WebView。但首先需要得到刻度值:
private int GetWebViewScale(){
int WebViewWidth = YourWebView.getWidth();
Double WebViewScale = new Double(WebViewWidth)/new Double(YourWebWidth); //Here, you must change YourWebWidth with your web width (in pixels)
WebViewScale = WebViewScale * 100d;
return WebViewScale.intValue();
}
现在您可以通过以下方式获取比例值:
YourWebView.setInitialScale(GetWebViewScale());
但是,如果你过早开始代码将不起作用,因此你将把该代码放在 WebViewClient 的 onLayoutChange 覆盖中,如下所示:
YourWebView.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
YourWebView.setInitialScale(getScale());
}
});
注意:您可能会遇到问题:(这两种情况都发生在我身上)
- WebView 稍微水平移动。 (#1)
- WebView 没有缩放。 (#2)
修复问题 #1:
- 只需在“YourWebWidth”值中添加一些像素。例如:
之前:
Double WebViewScale = new Double(WebViewWidth)/new Double(500); //(500 is my web's width)
之后:
Double WebViewScale = new Double(WebViewWidth)/new Double(505); //(500+5) - (This will add some padding at the left and right of your WebView. 2,5px at the left side; 2,5 at the right side)
修复问题 #2