1. 程式人生 > >如何優雅的計算webview渲染後的高度

如何優雅的計算webview渲染後的高度

final WebView cotent = (WebView) findViewById(R.id.cotent);
String baseUrl = "file:///android_res/drawable/";//讀取本地圖片路徑
cotent.loadDataWithBaseURL(baseUrl, infoForum.getContent() + "</br> <img src=\"/android_res/drawable/d_aini.png\"/>  ", "text/html", "utf-8", null);

cotent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                System.out.println("cotent.getHeight()===" + cotent.getHeight());//返回的是View的高度
                System.out.println("cotent.getContentHeight()===" + cotent.getContentHeight());//返回的是Html的高度
                System.out.println("cotent.getMeasuredHeight()===" + cotent.getMeasuredHeight());//返回的是View的高度
               
            }
        });