开发者

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. The iPhone version of this software uses the HTML meta-tag:

<meta name="viewport" content="width=320, initial-scale=0.95, maximum-scale=2.0, user-scalable=1">
开发者_C百科

Since Android's WebView doesn't seem to respect that tag I hard-coded the percent using setInitialScale(). However, the WebView is just flat-out ignoring this method call. No matter what number I put in there it shows at 100%.

Ideas?

Update: It's not working in the emulator, on my Droid (Motorola), or on my G1 (HTC).


Some HTC devices have problems with WebView, try this snippet!

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?

    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.


Can see several options.

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. I test on emulator 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.

3 . Maybe we use different SDKs. What SDK do you build against? I mean what is your target in default.properties? What is your uses-sdk in AndroidManifest.xml?

Here's a simple test that works fine for me 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 (,). 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜