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

标签 android html android-layout android-webview

我有纯文本数据,根据间距行等格式化。但我想将此文本显示为 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, 

我想像在网络上的 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
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</pre>

关于android - 需要在 HTML webview 中显示文本格式的数据,如 Stack Overflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35723872/

相关文章:

java - 在代码中一次性设置所有 int/imageresource/clicklistener ext

jquery - 如何调整 html 元素的位置

运行 MySQL 查询的 PHP 页面

android - 如何在更改 Activity 时保留 TextView 的内容

java - 检测用户手指触摸的大小

android - 适用于 Android 的 Adob​​e Flash 10 GPU 和 Adob​​e AIR

android - 更改 BottomNavigationView 的图标大小

android - 我在 Canvas 上绘图时是否可以显示图像?

android - Android Studio 中的 Gradle 构建错误

html - 使用 flex 在右侧显示 Logo 中心和菜单图标