Android, WebView.getWidth() == window.innerWidth

心不动则不痛 提交于 2019-12-11 08:26:26

问题


What combination of META tag settings in HTML and WebSettings in Java should I use to get the following result: window.innerWidth in Javascript should be always (in any Android version and on any device) equal to the WebView width in pixels.

For instance, I set the WebView width to 1024px. In Android AVD emulator, window.innerWidth is equal to 683px (2/3 of 1024, or 150% scale).

I tried the following META:

<meta name="viewport" content="width=device-width;
user-scalable=no; initial-scale=1.0;
minimum-scale=1.0; maximum-scale=1.0;" />

-- doesn't work.

Then I tried:

_WebView.getSettings().setLoadWithOverviewMode(true);
_WebView.getSettings().setUseWideViewPort(true);
_WebView.setInitialScale(100);

-- doesn't work too.

Finally,

_WebView.getSettings().setDefaultZoom(ZoomDensity.FAR);

seems to be working. According to documentation, it means "100% looking like in 240dpi". I don't know, what dpi users device has! How to get the task done correctly?

UPDATE

I've just tested ZoomDensity.FAR on a high-dpi tablet -- it works without it and doesn't work with it!

Regards,


回答1:


  1. META:

    meta name="viewport" content="width=device-width; user-scalable=no; initial-scale=1.0; minimum-scale=1.0; maximum-scale=1.0; target-densityDpi=device-dpi;"

  2. Java:

    _WebView.getSettings().setLoadWithOverviewMode(true); _WebView.getSettings().setUseWideViewPort(true);

This combination works.



来源:https://stackoverflow.com/questions/10272989/android-webview-getwidth-window-innerwidth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!