代码之家  ›  专栏  ›  技术社区  ›  Josh Alan

Android TextView不显示多行,即使字符串有换行符

  •  1
  • Josh Alan  · 技术社区  · 8 年前

    我的应用程序使用JSoup抓取一个网页,并从元素中获取文本以放入字符串。当我正常打印字符串时 System.out ,它可以打印多行。

    但是,当我使用 setText(myStringname) android:maxLines 到16岁。我用 articleDescs=articleDescs.replaceAll("(\\r|\\n|\\r\\n)+",System.getProperty("line.separator")); 用系统新行字符替换其他内容。

    假设我的TextView字符串是 Welcome to the TextView! \nWhat are we doing? \nPrinting new lines!

    Welcome to the TextView!
    What are we doing?
    Printing new lines!
    

    但它显示如下:

    Welcome to the TextView! What are we doing? Printing new lines!
    

    如何使其在多行上显示文本?我试着看其他问题,但他们让我这么做 maxLines ,我做到了。谢谢

    编辑:TextView XML

    <TextView
            android:id="@+id/itemDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_placeholder"
            android:maxLines="16"
            android:layout_marginLeft="10dp"/>
    
                String pretty=Jsoup.clean(aPrep,"",Whitelist.none().addTags("br","p"),new Document.OutputSettings().prettyPrint(true));
                String articleDescs=Jsoup.clean(pretty,"",Whitelist.none(),new Document.OutputSettings().prettyPrint(false));
                articleDescs=articleDescs.replaceAll("(\\r|\\n|\\r\\n)+","\n");
                System.out.println(articleDescs);
                String articleImgURLs=articleImgURLsArray;
                articleDescs=Jsoup.parse(articleDescs).text();
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Nabin Bhandari    8 年前

    我不认识周。但你似乎在清理线路中断。点击此链接: Preserve <br> tags when cleaning with Jsoup