簡體   English   中英

如何在android中獲取Webview的寬度和高度

[英]How to get width and height of a Webview in android

我想確定WebView的寬度和高度。 我已經嘗試過使用:

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

但結果日志總是顯示它們為 0。

這是一個更優雅的解決方案

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();


            }
        });



    }   

onPageFinished回調方法在使用loadDataloadDataWithBaseURL時不起作用。 我在Andromedev上看到了另一個使用 JavaScript 的解決方案,但我不相信這是最好的選擇。

對於任何仍在奮斗的人。 使用這些方法來獲取 webview 的高度和寬度。

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

您可能過早地檢查了尺寸,很可能是在onCreate 相反,試試這個:

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

嗯 - 我查看了 WevView 源代碼,我可以看到他們在內部使用 View#getWidth 和 View#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);

您過早地檢查 webview 的大小。 你可以用下面的方法試試這個

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

加載 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
//=========

我使用了一種解決方法,而不是從 DisplayMetrics 中獲取不可靠的 webview,假設您的 webview 寬度幾乎等於手機屏幕尺寸,高度幾乎等於手機屏幕尺寸的高度。

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = (int) displayMetrics.heightPixels*0.9;
int width = displayMetrics.widthPixels;

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

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

用這個。

我有同樣的問題。 我只是將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);
    }

我希望我的回答對某人有所幫助。

如果您想在加載內容后獲取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;
               }
       });

如果您想在加載內容后獲取原始高度,可以使用以下代碼。 雖然代碼已被棄用,但它運行良好。

webview.setPictureListener(new WebView.PictureListener() {
        @Override
        public void onNewPicture(WebView webView, @Nullable Picture picture) {
            int height = webview.getContentHeight();
            int height1 = webview.getMeasuredHeight();
            int height2 = webview.getHeight();
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM