前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android WebView

Android WebView

作者头像
码客说
发布2020-05-09 14:46:12
7170
发布2020-05-09 14:46:12
举报
文章被收录于专栏:码客码客

加载HTML文本

加载网页数据和本地文件合并后显示

src=>main=>assets目录下创建news_top.htmlnews_bottom.html

news_top.html

代码语言:javascript
复制
<!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

代码语言:javascript
复制
</body>
</html>

XML

代码语言:javascript
复制
<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

代码语言:javascript
复制
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-8gbkgb2312也一样。

代码语言:javascript
复制
webView.loadData(data, "text/html", "UTF -8");

解决方法

代码语言:javascript
复制
webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
webView.loadData(data, "text/html; charset=UTF-8", null);//这种写法可以正确解码

官方真是坑啊!!!

Utils

代码语言:javascript
复制
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();
    }
}

加载本地HTML文件

xieyi.html 放在 src=>main=>assets目录下

代码语言:javascript
复制
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");
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-10-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载HTML文本
  • 加载本地HTML文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档