加载网页数据和本地文件合并后显示
src=>main=>assets
目录下创建news_top.html
和news_bottom.html
news_top.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
body{
font-size: 16px;
line-height:200% !important;
letter-spacing:2px !important;
text-align: start;
word-wrap:break-word !important;
}
img{
max-width: 100% !important;
height: auto;
border-radius: 4px;
}
p{
padding: 0 !important;
}
</style>
</head>
<body>
<script type='text/javascript'>
window.onload = function()
{
var news_imgs = document.getElementsByTagName('img');
for(var i in news_imgs){
news_img[i].src = news_img[i].src+"!newinfo";
}
}
</script>
news_bottom.html
</body>
</html>
XML
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_weight="1"
android:background="@color/zj_gay_f9" />
Activity
fun initWebView(html: String) {
val webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
val tophtml = ZJFileUtils.ReadTxtFile(mContext, "news_top.html")
val bottomhtml = ZJFileUtils.ReadTxtFile(mContext, "news_bottom.html")
val html_all = tophtml + html + bottomhtml
webView.loadData(html_all, "text/html; charset=UTF-8", null)
}
中文乱码
使用 loadData
方法官方推荐的写法在部分手机上会中文乱码,即使指定utf-8
、gbk
、gb2312
也一样。
webView.loadData(data, "text/html", "UTF -8");
解决方法
webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
webView.loadData(data, "text/html; charset=UTF-8", null);//这种写法可以正确解码
官方真是坑啊!!!
Utils
public class ZJFileUtils {
//读取文本文件中的内容
public static String ReadTxtFile(Context mContext, String filename) {
StringBuilder sb = new StringBuilder();
//打开文件
try {
InputStream instream = mContext.getAssets().open(filename);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行读取
while ((line = buffreader.readLine()) != null) {
sb.append(line + "\n");
}
instream.close();
} catch (java.io.FileNotFoundException e) {
Log.d("TestFile", "The File doesn't not exist.");
} catch (IOException e) {
Log.d("TestFile", e.getMessage());
}
return sb.toString();
}
}
xieyi.html
放在 src=>main=>assets
目录下
fun initWebView() {
var webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(false);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.loadUrl("file:///android_asset/xieyi.html");
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有