报错信息
W/View: WebView not displayed because it is too large to fit into a software layer (or drawing cache), needs 10264320 bytes, only 9331200 available
报错现象:
我加载小于20KB的网页的时候,WebView也是能够正常加载的,但是当我加载一个180KB(不得不说测试真是鬼才)的网页时,就出现上面的报错了,且整个WebView是一片空白。
问题搜索核心关键词
- ScrollView中嵌套WebView空白
- View not displayed because it is too large to fit into a software layer
网上的答案:
动态创建WebView并add到容器中
- 参考国内:Android 开发用ScrollView 嵌套Webview 遇到坑坑 的小伙伴们
- 参考国外:https://stackoverflow.com/questions/15546416/how-to-shrink-webview-size-dynamically-according-to-its-content
Seems there's no way to resize the WebView(decrease) its size dynamically once its loaded. So, the only thing that could solve my problem was reloading the complete WebView.
So, instead of defining the Fragment in XML, I changed my stuff by adding Fragment dynamically every time. By which I was able to solve my issue.
个人感觉用处不大,我用的其他替代方案
大面积空白
参考:Android ScrollView嵌套WebView出现大面积空白页解决方法
对于其中的方案二我已经加上了,但是还是没解决我的问题。
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setVerticalScrollbarOverlay(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setHorizontalScrollbarOverlay(false);
用软件加速取代硬件加速
it happen for me when I change the Layer Type of the webview to use a software layer (default is hardware): mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
这个我原本就设置了,但是还是继续报错
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
或者在xml中设置layerType
也是有一样的效果的
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layerType="software" />
CoordinatorLayout 代替 ScrollView
Android ScrollView嵌套Webview(实际使用CoordinatorLayout嵌套Webview实现需求,效果都差不多)
这里就不赘述了,也是一种思路,但是和我的需求不一样。
给ScrollView 设置 layerType
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layerType="software">
也可以
mScrollView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);