具有wrap_content的Android 7 WebView [英] Android 7 WebView with wrap_content

查看:46
本文介绍了具有wrap_content的Android 7 WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ScrollView 内有一个 WebView android:layout_height ="wrap_content" .在Android 7之前,这导致WebView调整为我用 loadData 设置的本地html内容的高度.但是,在装有Android 7的Nexus 5X上,WebView的高度似乎并不可靠,有时只显示第一行文本的一部分,有时内容的末尾还有很大的空白.
我认为这可能是由于Google现在从牛轧糖开始使用Chrome for WebViews.有人对此问题有解释或解决方法吗?

I have a WebView with android:layout_height="wrap_content" inside a ScrollView. Prior to Android 7 this resulted in the WebView resizing to the height of the local html content I set with loadData. On my Nexus 5X with Android 7 though, the WebView height seems to be unreliable, sometimes it only shows parts of the first text line, sometimes there's a big gap at the end of the content.
I think it could be due to Google now using Chrome for WebViews starting with Nougat. Does anyone have an explanation or fix/workaround for this issue?

将视图包含在 RecyclerView 的单元格中也很重要.

It might also be important that the views are contained in the cells of a RecyclerView .

推荐答案

解决方法是:

等待html页面加载,然后在页面内运行JS以检测内容高度并将其设置为WebView Layout Parameters height.

waiting while html page will be loaded and run JS inside page to detect content height and set it to WebView Layout Parameters height.

在页面内运行JS很容易,只需导航到url

It is easy to run JS inside page, just navigate to url like

javascript:Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight);

要将结果传递给Java代码,您必须提供Java-Java Script接口,如此处所述https://developer.android.com/guide/webapps/webview.html (将JavaScript代码绑定到Android代码)

To pass the result to Java code you must provide Java-Java Script interface as described here https://developer.android.com/guide/webapps/webview.html (Binding JavaScript code to Android code)

您浏览的网址必须类似于

Your url to navigate must looks like

javascript:myfunc(Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.clientHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight));

将调用myfunc,您将获得页面的高度.现在只需将高度设置为WebView的高度.

The myfunc will be called and you will get the height of page. Now just set in height to WebView height.

这篇关于具有wrap_content的Android 7 WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆