我正在开发一个项目,需要在 WebView
中显示一些 HTML 字符串。问题是这些 HTML 字符串只包含 body 部分,没有任何像 <html>, <head>, <body>
这样的标签。当我尝试在 WebView 中显示接收到的字符串时,结果会显示从服务器接收到的 HTML 字符串中包含的标签。
以下是我所接收到和所做的一些示例:
从服务器接收到的 HTML 字符串:
05-30 14:02:56.785: D/(16970): body : <p align='justify'>The so-called Fiscal Compact is unlikely to be approved by the Latvian parliament in the second and final reading as four opposition MPs said that they would not back the initiative, local media reported on Tuesday evening. The second reading is scheduled for May 31. Under local legislation, the bill should be backed by 67 MPs in the 100-seat parliament, though the ruling coalition controls only 56 seats and depends on the opposition Union of Greens and Farmers (ZZS) to approve it. During the first reading 68 MPs backed the bill. The four MPs from ZZS will meet PM Dombrovskis later today to discuss the issue. They want more clarity about the country’s participation in neighboring Lithuania’s nuclear power plant project. Previously, ZZS said it extends its support on condition that the government fights for higher subsidies for farmers in the 2014-2020 EU budget framework.</p>
这是我的代码:
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + bodyTxt + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
结果如下所示:
我尝试使用 body.loadData(newBody, "text/html", "UTF-8");
,但这种方式甚至没有显示结果。
所以有什么想法是我做错了什么,我该怎么解决?
谢谢!
1oadData(String string, String string, String string)
不接受Html.fromHtml(string)
,因为它返回一个Spanned
对象。 - Matt