Ice Cream Sandwich 无法识别 HTML 中的换行符

发布于 2025-01-03 04:25:51 字数 1214 浏览 2 评论 0原文

这与我在此处提出的上一个问题有关。调用 replaceAll("\n", "
")
然后调用 Html.fromHtml() 将正确格式化预冰淇淋三明治中的文本;但是,正如您从 照片 中看到的那样,它在 ICS 中不起作用。我尝试了至少三十种不同的方法来捕获和替换文本中的换行符,但我却空手而归。我是否需要在 ICS 中调用某些特定内容,这是一个错误吗?其他人是否遇到过此问题?谁能想到一种解决方法,因为必须有一个。另外,任何人都可以提供一些关于为什么会发生这种情况的见解吗?这是非常奇特的。

需要明确的是:我已经在 Froyo 和 Gingerbread 上进行了测试,并且文本格式正确。在冰淇淋三明治中,情况并非如此。

以下是从 Last.fm 返回的文本示例。滚动到底部,它从“内容”开始。 Bon Iver URL

这是演示应用程序(如果您有兴趣亲自测试它)。 http://dl.dropbox.com/u/2301775/lastfm- api-test.zip

这里有两个屏幕截图说明了我的意思。它们来自测试应用程序。第一个运行的姜饼和第二个运行的冰淇淋三明治。

姜饼截图

冰淇淋三明治截图

This is in relation to a previous question I asked here. Calling replaceAll("\n", "<br />") and then Html.fromHtml() will properly format the text in pre-Ice Cream Sandwich; however, as you can see from the photo it does not work in ICS. I've tried at least thirty different ways to capture and replace the line breaks in the text, but I come up empty handed. Is there something in particular I need to call in ICS, is this a bug, has anyone else experienced this problem? Can anyone think of a work around, because there's got to be one. Also, can anyone offer some insight as to why this may be happening? It's very peculiar.

To be clear: I've tested this on Froyo and Gingerbread and the text formatted properly. In Ice Cream Sandwich, it does not.

Here's an example of the text being returned from Last.fm. Scroll to the bottom, it starts at "content". Bon Iver URL

Here's a demo app if you're interested in testing it first hand. http://dl.dropbox.com/u/2301775/lastfm-api-test.zip

Here are two screenshots illustrating what I mean. They are from the test app. The first running Gingerbread and the second running Ice Cream Sandwich.

Gingerbread screenshot

Ice Cream Sandwich screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2025-01-10 04:25:51

(删除了旧条目...测试了测试项目)

有一个非常有趣的“问题”,但我不确定谁负责。

我测试了你的应用程序并使其正常工作。
我正在查看 2.2 设备,其中第一个 \n 出现在字符串中(索引 413),然后我在 ICS 版本中查看了该索引。在那里我只能找到一个\r

您也可以在 LogCat 中看到差异。在 ICS 上,您会看到毫无意义的乱序文本,但在 ICS 下,您可以毫无问题地阅读完整文本。

因此,您也应该替换 \r,而不是用
替换 \n。为了确保安全,我会按以下顺序替换:

  1. \n\r
  2. \n
  3. \r

第 1 步对于消除这种可能性很重要 则“双倍”中断:

,如果有人使用 \n\r...使用的工作代码,

String artistText = artistInfo.getWikiText();
Log.i("Log", artistText);
artistText = artistText.replaceAll("\n\r", "<br />");
artistText = artistText.replaceAll("\n", "<br />");
artistText = artistText.replaceAll("\r", "<br />");
System.out.println(artistText);
txtWiki.setText(Html.fromHtml(artistText));

(removed old entry... tested the test project)

There is a extremely interesting "issue" but I am not sure who is responsible for it.

I tested your app and got it working.
I was looking on a 2.2 device, where the first \n appears (index 413) in the string and than I took a look at this index at the ICS version. There I could only find a \r.

You can see the difference in the LogCat, too. On ICS you see scrambled text with no sense but below ICS you can read the complete text without any problem.

So instead of replacing \n with <br/> you should replace \r, too. I would, just to make sure, replace in this order:

  1. \n\r
  2. \n
  3. \r

Step 1 is important to remove the possibility, that you "double" the breaks if someone uses the \n\r...

Used working code:

String artistText = artistInfo.getWikiText();
Log.i("Log", artistText);
artistText = artistText.replaceAll("\n\r", "<br />");
artistText = artistText.replaceAll("\n", "<br />");
artistText = artistText.replaceAll("\r", "<br />");
System.out.println(artistText);
txtWiki.setText(Html.fromHtml(artistText));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文