使用loadData时如何getContentHeight?

时间:2011-09-14 16:44:13

标签: android android-layout android-webview

我需要获取webview的高度,我使用静态内容加载,但返回的高度始终为零。

我最初认为这是因为在请求高度时没有加载webview,所以我尝试通过webview客户端在OnPageFinished中设置它。这在从Internet加载页面时起作用,但在加载本地内容时不起作用。

    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>

0 个答案:

没有答案