您的位置:首页 > 移动开发

Android WebView常见问题解决方案

2015-08-24 18:02 381 查看

1.webview 显示中文乱码

loadDataWithBaseURLwebview加载url时不会乱码,但是加载自己拼接的html代码会乱码,用loadDataWithBaseURL解决

webview.loadData(contentHtml.toString(), "text/html","utf-8");此方法显示中文乱码
webview.loadDataWithBaseURL(null, contentHtml.toString(), "text/html", "utf-8", null);

2.webview加载网页时支持LocalStorage

默认WebView没有开启LocalStorage存储。开启方法如下

webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCacheMaxSize(1024*1024*8);
webSettings.setDatabasePath(this.getCacheDir().getAbsolutePath());
注:setDatabasePath在API19时已经废弃,原因是因为在4.4WebView的内核已经换为了Chrome的内核,存储路径有WebView控制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: