【发布时间】:2012-11-28 18:46:14
【问题描述】:
我有一个包含以下内容的 html 字符串:
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="spanish press" content="spain, spanish newspaper, news,economy,politics,sports">
<title></title>
</head>
<body id="body">
<!-- The following code will render a clickable image ad in the page -->
<script src="http://www.myscript.com/a"></script>
</body>
</html>
我需要将该网站显示到 android 中的 webview 中。
我尝试了所有这些:
webView.loadDataWithBaseURL(null, txt, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("x-data://base", txt, "text/html", "UTF-8", null);
webView.loadDataWithBaseURL("notreal/", txt, "text/htm", "utf-8",null);
我也尝试删除 DOCTYPE 标签:
txt=txt.replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">", "");
没有一个人有工作。我刚刚实现了将字符串显示到 webview(html 代码)中,而不是必须使用该 html 代码创建的网站。
怎么了?
【问题讨论】:
-
你试过
loadData吗? -
你可以把这个html字符串放到文件res/values/strings.xml中并创建项目例如
<string name="website">your html here</string>然后通过Html.fromHtml(getString(R.string.website))加载它,如果你有时间请检查一下,因为我想知道它是否也有效;) -
(无论如何,你需要什么基本网址?)
-
@Lumma : Html.fromHtml 不适用于 webView。它返回一个用于 TextViews 的 Spanned。
标签: android webview android-webview