动态更改 HTML 文本用 WebView 加载

2,287 阅读1分钟
原文链接: youmu178.com

动态更改Html文本用WebView加载

By 04月20日 2015 android-development WebView

  • 在HTML中,需要更改的位置用占位符表示(名字随便起)
  • 把HTML转化成字符串,动态替换占位符
  • 用loadDataWithBaseURL加载

`   public static String htmlConvert(Context ctx) {
    StringBuilder sb = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(ctx.getAssets().open("play/view/information.html")));
        sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}`

  • 用WebView加载

    webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);