java - 渲染后,Android获取webview内容的高度

标签 java android webview

我正在尝试获取渲染后的 webview 的高度。它总是返回null,我试过getHeightgetMeasuredHeightgetContentHeight,它总是返回null。

布局:

<?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>

Activity

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 View 的高度,但我不明白为什么你不能。如果有人得到解决方案,将不胜感激。

最佳答案

您可以在该 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;
                   }
           });

关于java - 渲染后,Android获取webview内容的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22878069/

相关文章:

java - 当 viewpager fragment 的用户 "swipes out"时运行代码?

java - 更改 PDF 旋转文本的字体

java.io 和文件系统 block 大小

java - 按下警报对话框按钮始终返回 0 值

java - 单个 Activity 中的多个 Webview

java.lang.IllegalArgumentException : Number of scaling constants does not equal the number of of color or color/alpha components

android - 从 Activity 访问我自己的服务方法

java - onBindViewHolder 无法识别 View 持有者小部件

cocoa - 如何将 Webkit 插件添加到我的沙盒 OS X 应用程序 WebView 中?

Android Activity 到 WebView 和 WebView 到 Activity 的参数传递