android中WebView使用

android开发中有时候后台会返回一段html代码片段或者url链接,这些都被webview直接加载出来。

加载方式:

public void setEstateHtml(String htmlStr) {
        //不包含www格式的则为html片段
        //loadData()中的html data中不能包含'#', '%', '\', '?'这四中特殊字符
        if (!htmlStr.contains("www.")) {
            //去掉转义字符"\"
            String unescape = StringEscapeUtils.unescapeJava(htmlStr);
            typeWebView.loadData(SystemUtils.removeQuotation(unescape), "text/html", "UTF-8");
        } else {
            //加载url链接类型
            typeWebView.loadUrl(SystemUtils.removeQuotation(htmlStr));
        }
    }

去掉首尾引号:

  /**
     * 去掉首尾引号
     *
     * @param begin
     * @return
     */
    public static String removeQuotation(String begin) {
        String result = "";
        char[] begins = begin.toCharArray();
        int k = 0;
        int m = begins.length;
        if (begins != null) {
            char first = begins[0];
            char last = begins[m - 1];
            if (first == '"') {
                k = 1;
            }
            if (last == '"') {
                m = m - 1;
            }
        }
        result = begin.substring(k, m);
        return result;
    }
页面适应手机屏幕的分辨率,完整的显示在屏幕上
webView.getSettings().setLoadWithOverviewMode(true);

 

posted @ 2020-09-20 02:21  骷髅党政委  阅读(163)  评论(0编辑  收藏  举报