我有一个带有WebView的安卓应用程序(Android 10)。在那个WebView上,我必须使用一个固定位置的元素。现在我知道了,固定元素存在问题,但是HTML中的代码是这样的:
<meta name="viewport"
content="width=100%;
initial-scale=1;
maximum-scale=1;
minimum-scale=1;
user-scalable=no;">对于Webview来说:
WebView mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.loadUrl("path/to.html");我可以在使用变焦控制时放大。然而,多点触摸和平移会扭曲页面。
是否有可能禁用pich-和多点变焦,但保持变焦控制工作?
在Vikalp Patel的建议下,我得出了以下解决方案:
CustomWebView mWebView = (CustomWebView) findViewById(R.id.webView1);
mWebView.loadUrl("path/to.html");CustomWebView.java
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
public class CustomWebView extends WebView {
/**
* Constructor
*/
public CustomWebView(Context context) {
super(context);
}
/**
* Constructor
*/
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/*
* (non-Javadoc)
*
* @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
this.getSettings().setSupportZoom(false);
this.getSettings().setBuiltInZoomControls(false);
} else {
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
}
return super.onTouchEvent(event);
}
}在layout.xml中实现
<package.path.CustomWebView
...
/>希望,这对某人有帮助。
发布于 2013-01-03 12:18:59
我已经查看了WebView的源代码,并得出结论,没有优雅的方法来完成您所要求的任务。
最后我做的是子类化WebView和重写OnTouchEvent。
。在OnTouchEvent for ACTION_DOWN中,我检查有多少指针在使用MotionEvent.getPointerCount()。如果有多个指针,则调用setSupportZoom(false),否则调用setSupportZoom(true)。然后我打电话给super.OnTouchEvent()。
这将有效地禁用滚动时的缩放(从而禁用缩放控件),并在用户即将捏紧缩放时启用缩放。这不是一种很好的方法,但到目前为止对我来说效果很好。
请注意,getPointerCount()是在2.1中引入的,所以如果您支持1.6,您将不得不做一些额外的工作。
发布于 2013-01-03 12:23:00
Try to the following code
WebView mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.loadUrl("path/to.html");https://stackoverflow.com/questions/14138606
复制相似问题