2009-10-23 359 views
5

我想確定WebView的寬度和高度。我已經嘗試過使用:如何在android中獲取Webview的寬度和高度

webView.getWidth(); 
webView.getHeight(); 

但由此產生的日誌總是顯示他們爲0

+1

您可以發佈您的代碼?請記住,您無法在OnCreate方法中獲取視圖的寬度和高度,因爲尚未發佈佈局。 – Whaledawg 2009-10-23 14:17:03

+0

我把我的代碼在這裏height = mainWebView.getHeight(); System.err.println(「Display Total Height - >」+ mainWebView.getHeight()); System.err.println(「Measure Height - >」+ mainWebView.getMeasuredHeight()); – 2012-08-16 07:13:39

回答

1

嗯 - 我通過WevView源看去,我看到他們在內部使用查看#和的getWidth這裏查看#的getHeight年代WebView的私有方法之一:

/* 
* Return the width of the view where the content of WebView should render 
* to. 
*/ 
private int getViewWidth() { 
    if (!isVerticalScrollBarEnabled() || mOverlayVerticalScrollbar) { 
     return getWidth(); 
    } else { 
     return getWidth() - getVerticalScrollbarWidth(); 
    } 
} 

正如評論指出的,你必須確保你衡量它,你指定活動佈局如後setContentView(R.layout.mylayout);

1

您可能很快就會檢查尺寸,很可能是onCreate。使用loadDataloadDataWithBaseURL

webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onPageFinished(WebView webView, String url) { 
       super.onPageFinished(webView, url); 
       Log.i(TAG, findViewById(R.id.my_component).getWidth();); 
      } 
     }); 
+0

此方法不起作用,高度始終爲0. – Chan 2013-11-23 23:39:46

2

onPageFinished回調方法是行不通的:相反,試試這個。我在使用JavaScript看到了Andromedev上的另一個解決方案,但我不相信這是最好的方法。

0

如果你想獲得高度和寬度的加載時間,你不能得到它,因爲加載時間無法得到它。我建議使用webview Touchevent並獲取高度和寬度。否則,使用另一個視圖單擊事件並獲取高度和寬度。

webView.getWidth(); 
webView.getHeight(); 

使用此。

1

你正在檢查webview的大小太早。 您可以在下面的方法

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
      webView.getWidth(); 
      webView.getHeight(); 
} 
2

對於其他人誰仍在努力嘗試。 使用這些方法來獲取您的web視圖的高度和寬度。

computeHorizontalScrollRange(); -> for width 
computeVerticalScrollRange(); -> for height 
+1

請注意,這些方法可能會將不同的值返回給getWidth()和getHeight(),因爲它們返回整個可滾動寬度/高度,而不是屏幕上webview的實際寬度/高度。 – aaronmarino 2013-10-22 16:11:19

+0

另請注意,這些方法受到保護,例如,您可能很難從自定義佈局中使用它們。 – 2017-03-28 14:20:58

3

這裏有一個更優雅的解決方案

public void onCreate(Bundle savedInstanceState) { 


     webView = (WebView) findViewById(R.id.webView1); 

     webView.addOnLayoutChangeListener(new OnLayoutChangeListener() { 

      @Override 
      public void onLayoutChange(View v, int left, int top, int right,int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
       int w= webView.getWidth(); 
       int h= webView.getHeight(); 


      } 
     }); 



    } 
0

我有同樣的問題。我只是把initWebView方法在onWindowFocusChangedt

public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    initWebView(); 
} 


private void initWebView(){ 
    if(isOnline()){ 
     venueWebView = (WebView) findViewById(R.id.venueWebView); 
     String url = "some url"; 
     int viewWight = venueWebView.getWidth(); 
     int viewHeight = venueWebView.getHeight(); 
     venueWebView.loadUrl(url); 
    } 
0

我希望我的回答能幫助別人。

如果您想在加載內容後獲得webview的高度,那麼這可能會有幫助。

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; 
       } 
     }); 
0

加載HTML後,您需要WebView CONTENTS的寬度和高度。 是的,但沒有getContentWidth方法(僅查看端口值), 而且getContentHeight()不準確!

答:子類的WebView:

/* 
    Jon Goodwin 
*/ 
package com.example.html2pdf;//your package 

import android.content.Context; 
import android.util.AttributeSet; 
import android.webkit.WebView; 

class CustomWebView extends WebView 
{ 
    public int rawContentWidth = 0;       //unneeded 
    public int rawContentHeight = 0;       //unneeded 
    Context mContext   = null;      //unneeded 

    public CustomWebView(Context context)      //unused constructor 
    { 
     super(context); 
     mContext = this.getContext(); 
    } 

    public CustomWebView(Context context, AttributeSet attrs) //inflate constructor 
    { 
     super(context,attrs); 
     mContext = context; 
    } 

    public int getContentWidth() 
    { 
     int ret = super.computeHorizontalScrollRange();//working after load of page 
     rawContentWidth = ret; 
     return ret; 
    } 

    public int getContentHeight() 
    { 
     int ret = super.computeVerticalScrollRange(); //working after load of page 
     rawContentHeight = ret; 
     return ret; 
    } 
//========= 
}//class 
//=========