如何获取Android WebView中的完整高度? [英] How to get the full height of in android WebView?
问题描述
我有一个 RelativeLayout
和 WebView
.我正在将一些生成的html文本加载到该 WebView
中.加载数据后 WebView
高度超过屏幕.现在,我需要整个 WebView
的高度.直到现在,我尝试了 WebView#getHeight()
, WebView#getBottom()
,但是我得到了可见内容的高度.
I have a RelativeLayout
with WebView
. I am loading some generated html text into that WebView
. After loading the data WebView
height exceeding the screen. Now I need the height of the entire WebView
. Till now I tried WebView#getHeight()
, WebView#getBottom()
but I'm getting the height of visible content.
如何获取 WebView
的总高度?
推荐答案
最后,我得到了我的问题的答案.这是答案.
Finally I got the answer for my question. Here is the answer.
加载 WebView
后,我们将在 WebViewClient#onPageFinished
中获得调用,我们需要调用javascript以获取 WebView
的全部内容的高度.
After loading WebView
we will get call in WebViewClient#onPageFinished
there we need to call javascript to get the height of full content of WebView
WebViewClient
类
/**
* Custom web client to perform operations on WebView
*/
class WebClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:AndroidFunction.resize(document.body.scrollHeight)");
}
}
}
此后,我们将在 WebInterface
类中获取高度为高度的回调,该类已注册为 JavascriptInterface
After that we will get callback with height in WebInterface
class which is Registered as JavascriptInterface
/**
* WebView interface to communicate with Javascript
*/
public class WebAppInterface {
@JavascriptInterface
public void resize(final float height) {
float webViewHeight = (height * getResources().getDisplayMetrics().density);
//webViewHeight is the actual height of the WebView in pixels as per device screen density
}
}
这篇关于如何获取Android WebView中的完整高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!