WebView默认显示的是白色背景,与其它布局的默认颜色不统一,在网上找了一些方法,发现不行:
1、第一种方法
网上所谓的(无效)
android:layerType="software" 在Android5.1.中设置后,运行,整个WebView加载的网页都不显示
2、第二种方法(有错误)
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDefaultTextEncodingName("utf-8") ;
mWebView.setBackgroundColor(0); // 设置背景色
mWebView.getBackground().setAlpha(0); // 设置填充透明度 范围:0-255
mWebView.loadDataWithBaseURL(null, "加载中。。", "text/html", "utf-8",null);
mWebView.loadDataWithBaseURL(mGetDetail.data.get("hostsUrl"), mGetDetail.data.get("description"),
"text/html", "utf-8",null);
mWebView.setVisibility(View.VISIBLE); // 加载完之后进行设置显示,以免加载时初始化效果不好看
在Android5.1.中设置后,运行到这句
mWebView.getBackground().setAlpha(0);
// 设置填充透明度 范围:0-255
会报错,提示引用一个不存在的对象,可能跟我的布局有关系(我是将WebView嵌入在ListView中使用的,WebView作为ListView的Item中的内容)
3、在第二种方法基础上(成功)
布局文件如下
<WebView
android:id="@+id/webview99"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
java文件如下:
holder.webview.getSettings().setJavaScriptEnabled(true);
holder.webview.getSettings().setDefaultTextEncodingName("utf-8") ;
holder.webview.setBackgroundColor(0); // 设置背景色