的WebView返回坏window.innerHeightWebView、window、innerHeight

2023-09-06 02:44:07 作者:遗失的流年。

我有一个应用程序,它使用了Android的WebView的,以及一些JavaScript。当我WebViewClient电话 onPageFinished(),我提醒我的JavaScript运行初始化方法。我已启用JavaScript和加载之前我打 onPageFinished()

I have an application that makes use of the Android WebView, as well as some JavaScript. When my WebViewClient calls onPageFinished(), I alert my JavaScript to run an initializing method. My JavaScript is enabled and loaded before I hit onPageFinished().

在该JavaScript方法我使用 window.innerWidth 的。但是,它返回的值总是错的,总是相同的。不管我的方向就报告说,内部宽度为320,内部高度为240(用于纵向和横向正确的宽度值是360和598分别。)要是在别的地方我访问 window.innerWidth window.innerHeight 在JavaScript中它给了我一个准确的数字。

In that JavaScript method I make use of window.innerWidth. However, the value it returns is always wrong and always the same. Regardless of my orientation it reports that the inner width is 320, and the inner height is 240. (Correct width values for portrait and landscape are 360 and 598 respectively.) Anywhere else I access window.innerWidth or window.innerHeight in JavaScript it gives me an accurate number.

什么是更令人费解的是,如果我检查的WebView的高度或宽度直接在我的 onPageFinished()使用电话

What is more puzzling is that if I check the height or width of the WebView directly in my onPageFinished() call using

INT高度= view.getHeight();

INT宽度= view.getWidth();

那么它总是正确返回(虽然它返回准确的像素数,而不是DIP)。这让我觉得一切都已经完成加载的web视图,所以我不应该在我的JavaScript的任何问题。

then it always returns correctly (although it returns the exact pixel numbers, not the DIP). This makes me think that everything has finished loading with the WebView and so I shouldn't have any problems in my JavaScript.

任何想法,这是怎么回事?

Any ideas as to what is going on?

在此先感谢!

推荐答案

这是因为使用Javascript的WebView的查看相关的初始化之前执行。和Android的WebView返回一个320×240的默认值的JS。 执行你的JS经过一段时间就可以了,这样

This is because Javascript executes before WebView's View related initialization. And Android WebView returns a 320x240 default value to JS. Execute your JS after some time is ok, like this

function go() {
int height = view.getHeight();
int width = view.getWidth();
};
window.setTimeout(go, 300);