[英]Android WebView not respecting scaling percentage
I have a WebView which I'm trying to have scale to a certain percent on loading the page. 我有一个WebView ,我试图在加载页面时将其扩展到一定的百分比。 The iPhone version of this software uses the HTML meta-tag: 该软件的iPhone版本使用HTML元标记:
<meta name="viewport" content="width=320, initial-scale=0.95, maximum-scale=2.0, user-scalable=1">
Since Android's WebView doesn't seem to respect that tag I hard-coded the percent using setInitialScale() . 由于Android的WebView似乎不尊重该标签,因此我使用setInitialScale()对百分比进行了硬编码。 However, the WebView is just flat-out ignoring this method call. 但是,WebView只是忽略了这种方法调用。 No matter what number I put in there it shows at 100%. 不管我放在那里的数字是100%。
Ideas? 想法?
Update: It's not working in the emulator, on my Droid (Motorola), or on my G1 (HTC). 更新:它不能在模拟器,我的Droid(摩托罗拉)或我的G1(HTC)上运行。
Some HTC devices have problems with WebView
, try this snippet! 有些HTC设备有WebView
问题,试试这个片段吧!
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
myWebView = new WebView(this);
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setInitialScale(100);
try {
Method m = myWebView.getClass().getMethod("setIsCacheDrawBitmap", boolean.class);
if (m != null) {
m.invoke(myWebView, false);
myWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
myWebView.loadUrl("http://www.stackoverflow.com");
did you enable the zoom support for your webview? 你为webview启用了缩放支持吗?
WebSettings webSettings = mywebView.getSettings();
webSettings.setSupportZoom(true);
The code below helped me when mine wasn't responding. 当我没有回应时,下面的代码帮助了我。 Worth a shot: 值得一试:
mywebview.setWebViewClient(new WebViewClient());
mywebview.getSettings().setSupportZoom(true) ;
mywebview.getSettings().setUseWideViewPort(true) ;
mywebview.setInitialScale(1) ;
mywebview.loadUrl(sURL);
What version are you running? 你正在运行什么版本? It seems that 2.0 and below had an issue with this but was resolved in the 2.1. 似乎2.0及以下版本存在问题,但在2.1中得到了解决。
Can see several options. 可以看到几个选项。
1 . 1。 Probably there's something wrong with your code. 可能你的代码有问题。 Here's the most simple code and I can tell it works for me. 这是最简单的代码,我可以告诉它对我有用。 Content is scaled. 内容按比例缩放。
WebView webview1 = (WebView) findViewById(R.id.webview1);
webview1.setInitialScale(30);
webview1.loadUrl("http://stackoverflow.com");
So please post your code. 所以请发布您的代码。 All code related to WebView configuration. 所有与WebView配置相关的代码。 I test on emulator 2.2 我在模拟器2.2上测试
2 . 2。 Probably there's something wrong with url you test. 您测试的网址可能有问题。 You can test " http://stackoverflow.com " as I test with it and it is scaled fine. 您可以测试“ http://stackoverflow.com ”,因为我测试它并且它被缩放了。
3 . 3。 Maybe we use different SDKs. 也许我们使用不同的SDK。 What SDK do you build against? 您构建的SDK是什么? I mean what is your target in default.properties? 我的意思是default.properties中你的目标是什么? What is your uses-sdk in AndroidManifest.xml? 你在AndroidManifest.xml中的use-sdk是什么?
Here's a simple test that works fine for me http://open-pim.com/tmp/WebViewTest.zip . 这是一个适合我的简单测试http://open-pim.com/tmp/WebViewTest.zip 。 You may try it too. 你也可以尝试一下。
The meta
tag works for me, if I separate the parameters with semicolons (;) instead of commas (,). 如果我用分号(;)而不是逗号(,)分隔参数,那么meta
标记对我有用。 I've used this in the past: 我以前用过这个:
<meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
And it works, at least in the browser. 它起作用,至少在浏览器中是这样。 I'm not sure about a WebView
, but it might be worth a shot. 我不确定WebView
,但它可能值得一试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.