2014-04-05 60 views
13

我试图获取webview一旦呈现后的高度。它总是返回null,我试过getHeightgetMeasuredHeightgetContentHeight,它总是返回null。Android获取一次呈现的webview内容的高度

布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/instructions" 
    android:background="@color/transparent_black" > 

     <WebView 
      android:id="@+id/top_content" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 

活动

public class TestActivity extends MenuActivity { 

private final static String TAG = "HearingTest"; 
private String urlTopContent; 
private WebView topContent; 
private boolean mMoreInfoTop = true; 
private int mYdelta = 0; 
private int mBottomOffset = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.right_hearing_test); 

     String topHtml = this.getString(R.string.top_content); 
     //String bottomHtml = this.getString(R.string.bottom_content); 

     urlTopContent = "file:///android_asset/html/" + topHtml; 
     WebViewSettings(); 
     LoadWebPage(urlTopContent); 
    } 

    public void WebViewSettings(){ 

     topContent = (WebView) findViewById(R.id.top_content); 
     topContent.getSettings().setJavaScriptEnabled(true); 
     topContent.getSettings().setBuiltInZoomControls(true); 
     topContent.getSettings().setSupportZoom(true); 
     topContent.getSettings().setLoadWithOverviewMode(true); 
     topContent.setBackgroundColor(0); 
     topContent.canGoBack(); 

     int topHeight = topContent.getContentHeight(); 
     Log.d("Top Height", "Height: " + topHeight);    

     topContent.setWebViewClient(new WebViewClient(){ 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (Uri.parse(url).getHost().equals(urlTopContent)) { 
        // This is my web site, so do not override; let my WebView load the page 
        return false; 
       } 
       // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
       startActivity(intent); 
       return true; 
      } 

      @Override 
      public void onPageFinished(WebView view, String url) { 
       View webViewHeight = (View) findViewById(R.id.top_content); 
       int height = webViewHeight.getHeight(); 
       Log.d("Top Content","Top Content Height:" + height); 
      } 
     }); 
    } 

    public void LoadWebPage(String url){ 

     try { 
      topContent.loadUrl(url); 
      Log.d("Loading Web Page", "URL" + url + "connected"); 
     } 
     catch (Exception e) { 
       Log.d("Loading Web Page", "URL: " + url + " couldn't connect."); 
     } 
    } 

} 

我甚至不知道是否有可能得到Web视图的高度,它已经呈现后,但我不你明白为什么你不能。如果有人得到和解决方案,将不胜感激。

回答

25

您可以使用该WebView上的ViewTreeObserver来获取渲染其内容后的实际高度。

这里是示例代码。

ViewTreeObserver viewTreeObserver = mWebView.getViewTreeObserver(); 

viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() { 
        @Override 
        public boolean onPreDraw() {         
          int height = mWebView.getMeasuredHeight(); 
          if(height != 0){ 
            Toast.makeText(getActivity(), "height:"+height,Toast.LENGTH_SHORT).show(); 
            mWebView.getViewTreeObserver().removeOnPreDrawListener(this); 
          } 
          return false; 
        } 
      }); 
+3

像一个魅力工作,感谢很多年来一直坚持这一点。 – Paddy1990

+0

onloadPageFinish之后在哪里调用? – NovusMobile

+0

我得到错误的价值..测试www.google.com给予大小714px – NovusMobile

0

屡试不爽以下:

mWebView = new WebView(this); 
    mWebView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 

     @Override 
     public void onLayoutChange(View view, int l, int t, int r, int b, int ol, int ot, int or, int ob) { 
      Log.i("demo", String.format("%s: top=%d, bottom=%d, content height=%d", 
        Thread.currentThread().getStackTrace()[2].getMethodName(), t, b, mWebView.getContentHeight() 
      )); 
     } 

    }); 

日志:

12-20 16:08:33.969 1466年至1466年/ yourPkg I /演示:onLayoutChange:顶= 0 , bottom = 0,content height = 0
12-20 16:08:33.970 1466-1466/yourPkg I/demo:onLayoutChange:top = 0,bottom = 0,content height = 0
12-20 1 6:08:34.061 1466-1466/yourPkg I/demo:onLayoutChange:top = 0, bottom = 1510,content height = 0
12-20 16:08:34.091 1466-1466/yourPkg I/demo:onLayoutChange:顶部= 0,底部= 1510,内容高度= 2050

最后一个是正确的。
ViewTreeObserver有时候给了我错误的身高。