单击 Web 视图中的表单字段时禁用缩放?
我已经浏览了几十页类似的问题,但没有一个有任何答案,所以希望这一页会有所不同。
我有一个网络视图,我不希望视图的缩放从我设置的初始缩放级别发生变化。 当前唯一改变缩放级别的是文本框聚焦时。
我需要能够通过 Java 代码来完成此操作,而不是使用视口元标记。
只是为了让我没有常见的响应,我在代码中添加了以下内容来禁用缩放和缩放控件:
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);
我认为可能的解决方案是检查 onFocus 甚至 onClick 事件何时发生在WebView 然后 ZoomOut,但我什至不确定这是否可能?
任何建议将不胜感激。
I've looked through dozens of pages if similar questions, none of them have any answers, so hopefully this one will be different.
I have a webview, and I do not want the zoom of the view to change from the initial zoom level I have it set to. The only thing which changes the zoom level currently is when a text box is focused.
I need to be able to do this through Java code, not using the viewport meta tag.
Just so I don't have the common responses, I have the following in my code to disable zooming, and the zoom controls:
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);
I'm thinking that a possible solution is to check to see when an onFocus or even an onClick event occurs within the WebView and then zoomOut, but I'm not even sure if that is possible?
Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更新 这个答案是差不多6年前写的,从那时起所有新的android版本都出现了,这很可能已经过时了。
这件事引起了很大的头痛,但最后感谢
setDefaultZoom(ZoomDensity.FAR);
解决了这一问题,重要的是
onCreate
和loadUrl
在 WebSettings 之前调用,否则它造成了强制关闭的情况。这里是整个代码,包括导入(对于 Java 新手用户)UPDATE This answer was written almost 6 years ago, with all the new android versions that came since then, this is most likely outdated.
This thing caused a major headache, but finally was solved thanks to
setDefaultZoom(ZoomDensity.FAR);
One thing which is important is that
onCreate
andloadUrl
get called before the WebSettings, otherwise it caused a force close situation. Here the ENTIRE code including imports (for the novice Java users)我在 HTC 手机上通过添加一个带有 onScaleChanged 空监听器的 WebViewClient 解决了这个问题。我的应用程序是 PhoneGap,所以这就是它的样子,但是添加侦听器在非 PhoneGap 应用程序中应该看起来相同:
}
奇怪的是,onScaleChange 侦听器永远不会被调用 - 通过侦听缩放,它会阻止缩放正在发生。我发现我需要所有其他调用(setSupportZoom、setDefaultZoom、setInitialScale)才能使其正常工作,删除它们中的任何一个都会恢复到旧的、有缺陷的行为。
I solved this on HTC phones by adding a WebViewClient with an empty listener for onScaleChanged. My app is PhoneGap, so this is what it looks like, but adding the listener should look the same in a non-PhoneGap app:
}
Strangely, the onScaleChange listener never gets called -- by listening for the zoom, it blocks the zoom from happening. I've found that I need all the other calls (setSupportZoom, setDefaultZoom, setInitialScale) in order for this to work, and removing any of them reverts to the old, buggy behavior.
我也有同样的麻烦。我需要找到一种方法将 webview 的内容缩放到精确值,一切正常,直到用户开始输入文本。有些方法适用于相对较新的 Android 4.0+ 设备,但在旧设备上失败。在任何地方都有效的唯一方法是设置缩放值,而不是在 Java 中,而是在视口中设置,如下所示
它适用于我测试的每个设备。
I had the same trouble. I needed to find a way to scale content of webview to exact value, everything worked fine until user starts to input text. There are methods that work on relatively new devices android 4.0+ but fails on old ones. The only way that works everywhere is setting the zoom value not in Java but in viewport like this
It works on every device I tested.
您是否尝试在视口标签中禁用用户可缩放?不确定这是否对你有用,但对我有用。我不需要在java方面做任何事情。
Did you try to disable the user-scalable in the viewport tag? Not sure if that will work for you, but it works for me. I did not need to do anything on the java side.
我也遇到过这个问题,我是这样解决的:
在三星Galaxy Tab上运行正常。我希望这会对您有所帮助。
I have encountered this problem too, and I solved it like this:
It's runing normally on Sumsung Galaxy Tab. I hope this will help you.
WebView 有一个特殊的“东西”,我认为它会在这里引发许多问题和答案。发生的情况是,当加载 URL 时,默认的 Android 浏览器会通过 Intent 启动来处理此问题。 缩放在此浏览器中进行,而不是在您的 Web 视图中进行。
解决方案:您需要添加一个 WebviewClient 来告诉 Android 您处理自己浏览一下。示例:
我的 main.xml 如下所示
此代码禁用了运行 Android 2.2 的 HTC Desire 的缩放功能。点击 HTML 输入字段没有什么区别。
WebView/HelloWebViewClient 的整个主题以及正确处理“后退”按钮的重要提示都记录在 你好视图,Web 视图。对于任何使用 WebView 的人来说,这应该是必读的一本书。
The WebView has one special "thing", which I think it will trigger many questions and answers here. What happens is, that when an URL is loaded, the default Android Browser kicks in through an Intent to handle this. The zooming takes part in this browser, not in your Webview.
Solution: You need to add a WebviewClient to tell Android that you handle the browsing yourself. An example:
My main.xml looks like this
This code disabled zooming on my HTC Desire running Android 2.2. Tapping into HTML Input fields makes no difference.
The whole topic of WebView/HelloWebViewClient as well as an important hint to handle the "Back" button correctly is documented in Hello Views, Web View. It should be required reading for anybody who uses WebView.
我相信您可以使用
WebView.setInitialScale
方法设置缩放级别。它需要一个 int 作为比例,所以我猜你会想要做类似myWebView.setInitialScale(100)
的事情。I believe you can set the zoom level with
WebView.setInitialScale
method. It takes an int as scale so I guess you would want to do something likemyWebView.setInitialScale(100)
.此问题已通过 HTC 设备上的固件更新得到解决,它(显然)是由 Sense UI 错误地覆盖默认 Android 功能引起的。
很难准确地提供有关此问题何时得到纠正的信息,但是当在具有最新固件的任何 HTC 设备上单击文本框时,我的 Web 应用程序不再缩放。
以下两行代码将禁用 android webview 的“缩放”功能:
This issue has been fixed by a firmware update on HTC devices, it was (apparently) being caused by the Sense UI overriding default Android functionality incorrectly.
It is very difficult to provide information on exactly when this was corrected, however my web application no longer zooms when a text box is clicked on any HTC device with the latest firmware.
The following two lines of code will disable the "zoom" aspects of an android webview:
这也让我头疼,但幸运的是我找到了这篇文章:如何在移动设备上停止放大输入焦点。
在 css 文件中将输入元素中文本的字体大小设置为 16px(或更大)。
这是相当黑客,但如果没有其他工作......
This was headache for me too, but fortunately I have found this article: How to stop zoom in on input focus on mobile devices.
Set font size of the text in the input element to 16px (or more) in the css file.
It is rather hack, but if nothig else works ...