我有一个RelativeLayout带WebView.我正在加载一些生成的HTML文本WebView.加载WebView超过屏幕的数据高度后.现在我需要整个高度WebView.直到现在我试过了WebView#getHeight(),WebView#getBottom()但我得到了可见内容的高度.
最后我得到了我的问题的答案.这是答案.
加载后WebView我们将在WebViewClient#onPageFinished那里打电话,我们需要调用javascript来获取全部内容的高度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)");
}
}
}
Run Code Online (Sandbox Code Playgroud)
之后我们将在WebInterface课程中获得高度回调,注册为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
}
}
Run Code Online (Sandbox Code Playgroud)
将您包裹WebView在 a 中ScrollView,webView.getHeight()并将返回实际 WebView 内容的高度。确保您WebView的高度属性设置为wrap_content.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9977 次 |
| 最近记录: |