WebView显示带有loadDataWithBaseURL的源html,而不是呈现的视图 [英] WebView shows source html with loadDataWithBaseURL, not rendered view

查看:142
本文介绍了WebView显示带有loadDataWithBaseURL的源html,而不是呈现的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它使用WebView呈现自定义html. 但是当我打电话
loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", "text/html; charset=utf-8;", "utf-8", null);
它在Genymotion模拟器上显示html本身(未呈现). 在我的HTC-one上,它可以与渲染的html一起正常工作. 每个结果均显示为附件.

I'm developing an application witch uses WebView to render custom html. But when I call
loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", "text/html; charset=utf-8;", "utf-8", null);
it shows html itself (not rendered one) on Genymotion emulator. On my HTC-one, it works fine with rendered html. Each result is showed as attached.

有人有相同的问题或解决方案吗? 谢谢.

Does anyone have a same problem or solution? Thanks.

推荐答案

请勿在KitKat下面输入mimeType.

Don't enter mimeType below KitKat.

fun getMimeType(): String? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        "text/html; charset=utf-8"
    } else {
        null
    }
}
loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", getMimeType(), "utf-8", null);

Java:

if(Build.VERSION.SDK_INT < 21)
    webView.loadDataWithBaseURL("about:blank","<html><h1>TEST</h1></html>","text/html", "UTF-8",null);
else
    webView.loadDataWithBaseURL("about:blank","<html><h1>TEST</h1></html>","text/html; charset=utf-8", "UTF-8",null);

这篇关于WebView显示带有loadDataWithBaseURL的源html,而不是呈现的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆