Android 禁用WebView双选项卡缩放
我正在Android网络视图中显示一个网页。页面需要缩放以适应WebView的可用宽度 HTML(宽度550只是一个示例,可能会改变): 除了一个问题外,这工作正常:双制表符触发概览模式和默认比例之间的切换。Android 禁用WebView双选项卡缩放,android,html,webview,Android,Html,Webview,我正在Android网络视图中显示一个网页。页面需要缩放以适应WebView的可用宽度 HTML(宽度550只是一个示例,可能会改变): 除了一个问题外,这工作正常:双制表符触发概览模式和默认比例之间的切换。 有没有办法禁用此功能 顺便说一句:我无法计算代码中的比例,因为加载的页面可能有不同的大小,这是我事先不知道的。我还检查了WebView的源代码,但没有看到任何可以重写或更改的内容。一般来说,更改已知的API不是好做法。您可能应该找到一种不同的方法,也许是longClick 总之,请看。一般
有没有办法禁用此功能
顺便说一句:我无法计算代码中的比例,因为加载的页面可能有不同的大小,这是我事先不知道的。我还检查了WebView的源代码,但没有看到任何可以重写或更改的内容。一般来说,更改已知的API不是好做法。您可能应该找到一种不同的方法,也许是
longClick
总之,请看。一般来说,更改已知的API不是好的做法。您可能应该找到一种不同的方法,也许是
longClick
无论如何,请看。您可以编写自己的WebView实现来实现EstureListener:
public class MyWebView extends WebView implements OnGestureListener
在手势检测器中声明:
private GestureDetector gestureDetector;
实现initWebView()方法并在构造函数中调用它,如下所示:
// Best you do this for every WebViewConstructor:
public AppWebView(Context context) {
super(context);
initWebView(); }
private void initWebView(){
gestureDetector = new GestureDetector(getContext(), this);
// Do any other customizations for your webview if you like.
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return true;
return super.onTouchEvent(event);
}
现在从OnTestureListener实现方法,并在双击事件时返回true:
public boolean onSingleTapConfirmed(MotionEvent e) {
return false; //Nothing
}
public boolean onDoubleTap(MotionEvent e) {
return true; //Nothing
}
public boolean onDoubleTapEvent(MotionEvent e) {
return true; //Nothing
}
public boolean onDown(MotionEvent e) {
return false; //Nothing
}
public void onShowPress(MotionEvent e) {
//Nothing
}
public boolean onSingleTapUp(MotionEvent e) {
return false; //Nothing
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false; //Nothing
}
public void onLongPress(MotionEvent e) {
//Nothing
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false; //Nothing
}
最后覆盖webview的OnTouchEvent,并让它通过事件传递到手势检测器,如下所示:
// Best you do this for every WebViewConstructor:
public AppWebView(Context context) {
super(context);
initWebView(); }
private void initWebView(){
gestureDetector = new GestureDetector(getContext(), this);
// Do any other customizations for your webview if you like.
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return true;
return super.onTouchEvent(event);
}
这通常效果很好,但此解决方案有一个基本缺陷:一些未被识别为DoubleTap事件的事件可能会导致webview的缩放
我仍在为此制定解决方案,并将在此处发布我的进展:
您可以编写自己的WebView实现来实现EstureListener:
public class MyWebView extends WebView implements OnGestureListener
在手势检测器中声明:
private GestureDetector gestureDetector;
实现initWebView()方法并在构造函数中调用它,如下所示:
// Best you do this for every WebViewConstructor:
public AppWebView(Context context) {
super(context);
initWebView(); }
private void initWebView(){
gestureDetector = new GestureDetector(getContext(), this);
// Do any other customizations for your webview if you like.
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return true;
return super.onTouchEvent(event);
}
现在从OnTestureListener实现方法,并在双击事件时返回true:
public boolean onSingleTapConfirmed(MotionEvent e) {
return false; //Nothing
}
public boolean onDoubleTap(MotionEvent e) {
return true; //Nothing
}
public boolean onDoubleTapEvent(MotionEvent e) {
return true; //Nothing
}
public boolean onDown(MotionEvent e) {
return false; //Nothing
}
public void onShowPress(MotionEvent e) {
//Nothing
}
public boolean onSingleTapUp(MotionEvent e) {
return false; //Nothing
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false; //Nothing
}
public void onLongPress(MotionEvent e) {
//Nothing
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false; //Nothing
}
最后覆盖webview的OnTouchEvent,并让它通过事件传递到手势检测器,如下所示:
// Best you do this for every WebViewConstructor:
public AppWebView(Context context) {
super(context);
initWebView(); }
private void initWebView(){
gestureDetector = new GestureDetector(getContext(), this);
// Do any other customizations for your webview if you like.
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) return true;
return super.onTouchEvent(event);
}
这通常效果很好,但此解决方案有一个基本缺陷:一些未被识别为DoubleTap事件的事件可能会导致webview的缩放
我仍在为此制定解决方案,并将在此处发布我的进展:
如果可能,替换html中的静态元标记:
<script>
var meta = document.createElement("meta");
meta.setAttribute('name','viewport');
meta.setAttribute('content','width=device-width, user-scalable=no');
document.getElementsByTagName('head')[0].appendChild(meta);
</script>
重写onTouchEvent方法(在自定义WebView中):
如果可能,请替换html中的静态元标记:
<script>
var meta = document.createElement("meta");
meta.setAttribute('name','viewport');
meta.setAttribute('content','width=device-width, user-scalable=no');
document.getElementsByTagName('head')[0].appendChild(meta);
</script>
重写onTouchEvent方法(在自定义WebView中):
我没有添加双标签。它内置在WebView中,因此我无法更改它。无论如何,我不想添加它,我想禁用它。正如我所说的,在本机UI中禁用nuilt是一种糟糕的做法。双标签不是我添加的东西。它内置在WebView中,因此我无法更改它。无论如何,我不想添加它,我想禁用它。正如我所说的,在本机UI中禁用nuilt是不好的做法,这看起来很有希望。我将接受它作为答案,而不进行测试。我做了一个黑客攻击,我完全禁用了缩放,将WebView传递给HTML,并将内容放入javascript中。不是很好,但是很有效。Thx,看起来很有希望。我将接受它作为答案,而不进行测试。我做了一个黑客攻击,我完全禁用了缩放,将WebView传递给HTML,并将内容放入javascript中。不是很好,但是很管用。