使用loadData时如何获取ContentHeight?
我需要获取 webview 的高度,我加载了静态内容,但返回的高度始终为零。
我最初以为是因为请求高度时webview没有加载,所以我尝试通过webview客户端在OnPageFinished中设置它。这在从互联网加载页面时有效,但在加载本地内容时无效。
String html = "<html><head><title>Test</title></head><body><p>Insert content here</p></body></html>";
qWebView = (WebView) findViewById(R.id.webView1);
qWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
qWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
int height = qWebView.getContentHeight();
Toast.makeText(WebviewHeightTestActivity.this, String.valueOf(height), Toast.LENGTH_SHORT).show();
}
});
。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="wrap_content"></WebView>
I need to get the height of my webview, which I load with static content, but the height returned is always zero.
I initially thought it was because the webview hadn't loaded when requesting the height, so I tried setting it in OnPageFinished through a webview client. This works when loading a page from the internet, but not when loading local content.
String html = "<html><head><title>Test</title></head><body><p>Insert content here</p></body></html>";
qWebView = (WebView) findViewById(R.id.webView1);
qWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
qWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
int height = qWebView.getContentHeight();
Toast.makeText(WebviewHeightTestActivity.this, String.valueOf(height), Toast.LENGTH_SHORT).show();
}
});
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="wrap_content"></WebView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论