其他分享
首页 > 其他分享> > android-需要在HTML webview中显示文本格式的数据,例如Stack Overflow

android-需要在HTML webview中显示文本格式的数据,例如Stack Overflow

作者:互联网

我有用间隔线等格式设置的纯文本数据.但是我想将此文本显示为HTML吗?如何才能做到这一点.

[American Thinker]

 March 1, 2016
 [American Thinker] [Navigation Menu]
 HomeArchivesVideoCartoonsAboutSearchLoginRules/FAQContactDonations
 Merchandise <http://www.americanmethod.com/american-thinker/>
 Home Archives Video Cartoons About Search Login More [Down Arrow]
   * Rules/FAQ
   * Contact
   * Donations
   * Merchandise <http://www.americanmethod.com/american-thinker/>


 American Thinker Blog

 Mexican oil giant devastated by oil price decline - 3/1/16 March 1, 2016 
 The state owned oil company that provides one third of the revenue of the 
 Mexican government is in a deep crisis, owing to low oil prices, 

我想显示的内容就像在Web上stackoverflow上显示的一样.

问题是,当我说一个段落时,它看起来都串在一起.

支持相反的操作Html.fromHtml(htmlString),但是如何将文本转换为可以在HTML中正确显示的内容?直到我发布才想到它,但真的希望它看起来像此编辑文本.

更新:

    <PRE> </PRE>  

是否执行了我想要的但现在没有换行的操作,所以必须向右或向左滚动.

更新:根据下面的建议添加内容,现在我得到了换行符,但有些行如下所示:

    The state owned oil company that
    provides one third of the revenue of                                      
    the
    Mexican government is in a deep crisis,
    owing to low oil prices,
    inefficiency, and corruption. The
    implications for the United States are
    important.  More
    The Calamitous Climate at
    etc etc

这是我的html:

       String htmlstuff = " \n" +
              "<html>\n" +
              "<header><title>This is title</title></header>\n" +
              "<body>\n" +


        "<pre style=\"white-space: pre-wrap;\">\n";

        htmlstuff=htmlstuff+result;
        htmlstuff=htmlstuff+
                " </pre>\n"+

              "</body>\n" +
              "</html>\n";
        wv.loadDataWithBaseURL("", htmlstuff, "text/html", "UTF-8", "");

        wv.setHorizontalScrollBarEnabled(false);

解决方法:

考虑这个Html display formatted text

您只需要将文字放在

<pre>

标记并使用CSS

white-space: pre-wrap;

属性以不显示水平滚动来显示它

<pre style="white-space: pre-wrap;">
    Formated text
    Line 2      with wide spaces
</pre>

对于演示,请检查this.

标签:html5,android-layout,android-webview,html,android
来源: https://codeday.me/bug/20191119/2032475.html