GetContentHeight() does not work properly

前端 未结 2 970
北荒
北荒 2021-01-13 19:41

I\'m trying to retrieve the height of a webview\'s content. it contains a html-string. The problem is that getContentHeight() returns always the same value (392), no matter

相关标签:
2条回答
  • 2021-01-13 20:00

    I had this problem with getContetHeight() as well. First in the WebViewClient.OnPageFinish() callback the content height wasn't already set so what I did it's pretty rought solution.

    Handler h;
    Runnable scroll_updater = new Runnable() {
        @Override
        public void run() {
            if(Thread.interrupted())
                return;
    
            if(lyrics.getContentHeight() == 0)
                h.postDelayed(this, 100);
    
            setLyricsScroll();
        }
    };
    
    0 讨论(0)
  • 2021-01-13 20:23

    This issue has driven me nuts. Let me make a guess: Your content isn't loaded from the web or from the file system instead you make a call to WebView.loadData() or WebView.loadDataWithBaseUrl() (where you specify null or an empty string in the latter case). Right?

    If yes, then I guess I have a solution for you which was inspired by this discussion: Be sure to use the WebView.loadDataWithBaseUrl() method and provide a baseUrl which is not null, not an empty string and is different for different content data. According to the SDK documentation the baseUrl is only used to evaluate relative URLs inside the data (e.g. <img> tags not specifying the domain name in their src attribute), so if your data does not reference external content I guess you can generate "virtual" URLs (haven't tried this).

    It seems as if the WebView is taking a look at the URL and if it doesn't change all subsequent calls to loadData... will result in the same height of the WebView.

    0 讨论(0)
提交回复
热议问题