【问题标题】:\n not working in textview android\n 不能在 textview android 中工作
【发布时间】:2015-01-23 13:55:37
【问题描述】:

我正在开发一个应用程序,在该应用程序中,我使用站点中的 jsoup 解析 XML 文件并将其显示在 textview 上。我遇到的问题是 RSS 中包含的格式说明符 \n 不是 wprking。它没有进入新行,而是按原样显示 \n 。这是我的代码

    hello = sb.toString();
        String title, description = null;
        Document document = Jsoup.parse(hello);
        Elements a = document.getElementsByTag("item");
        for (Element element : a) {
            title = element.child(0).text();
            description = element.getElementsByTag("description").get(0).text();
            String src = Jsoup.parse(description).select("img").first().attr("src");
            String id = Jsoup.parse(description).select("id").text();
              description = Jsoup.parse(description).text();

               description = description.replace(id, "");
              description =  description.replace("/", "\\");

            list.add(new News(title,  id, src, description ));

描述包含 \n 标签,但在文本视图中它不工作,如图所示。正如您在第一行中看到的那样,而不是按原样进入新行 \n 。

【问题讨论】:

  • @TechnoCracker 我是 android 新手.. 你能解释一下吗?
  • 你的描述存储在哪里?
  • 无处可去。我只是解析并在屏幕上显示它。
  • 我给你举个例子... tv1.setText(Html.fromHtml("Name" + "
    " + "Chirag Savsani"));

  • 输出显示后换行 n 换行...

标签: android xml-parsing jsoup format-specifiers


【解决方案1】:

试试这个:

 description= description.replaceAll("\\n", System.getProperty("line.separator"));

因为斜线 \ 必须是一种“castet”,所以你可以用一个斜线 \ 来做到这一点。

【讨论】:

  • 为什么要替换 \\\n 而不是 \n?
  • 你必须用两个斜线来投射一个斜线 \\,我想,不确定但如果它不起作用,请删除一个斜线并告诉我,以更正答案跨度>
  • 我刚刚添加了您的代码,但它不起作用.. 在添加您的代码之前我需要做一些其他事情吗?实际上我有 \n 而不是 \\n.
  • 你能给我一个例子,原始的描述字符串是怎样的吗?我的意思是没有任何替代品..
  • 它与我在问题中添加的图中显示的相同。
【解决方案2】:

也许添加另一个替换调用以将"\\n" 的实例替换为"\n"

【讨论】:

  • 也许甚至可以创建一个无限循环的替换?如果第一次不成功,为什么第二次会成功?
  • 我输入了 2 个反斜杠,但由于某种原因,答案只呈现了一个
  • 使用replace("\\n", "\n");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 2016-05-13
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
相关资源
最近更新 更多