How to have HTML New Line Feature in Android Text View?

梦想的初衷 提交于 2019-12-30 05:43:52

问题


I am using webservice in my android application. In my webservice result I get a HTML formatted String as result.

Assume this is my web service result:

Books on Chennai:\n

  • Madras Discovered, Tales of Old and New Madras, Madras (1992) by S. Muthiah
  • \n
  • Madras – its Past and its Present (1995) by S. Muthiah
  • \n
  • Madras – its Yesterdays and Todays and Tomorrows by S. Muthiah
  • \n
  • At Home in Madras (1989) by S. Muthiah
  • \n
  • The Spirit of Chepauk (1998) by S. Muthiah
  • \n
  • The Story Of Fort St. George (1945) by Col. D.M. Reid
  • \nFiction set in Chennai\n
  • Kalyani’s Husband by S. Y. Krishnaswamy
  • \n
  • Chasing Rainbows in Chennai (http://chasingrainbowsinchennai.blogspot.com/\">Chasing Rainbows in Chennai), (2003) by Colin Todhunter
  • \n
  • In Old Madras (1914) by Bithia Mary Crocker
  • \n

I am using Html.fromHtml(String) to add this to my TextView. But I am not getting new line feature. I displays it as a paragraph.

Books on Chennai: Madras Discovered, Tales of Old and New Madras, Madras (1992) by S. Muthiah Madras – its Past and its Present (1995) by S. Muthiah Madras – its Yesterdays and Todays and Tomorrows by S. Muthiah At Home in Madras (1989) by S. Muthiah The Spirit of Chepauk (1998) by S. Muthiah The Story Of Fort St. George (1945) by Col. D.M. ReidFiction set in Chennai Kalyani’s Husband by S.Y. Krishnaswamy Chasing Rainbows in Chennai (Chasing Rainbows in Chennai), (2003) by Colin Todhunter In Old Madras (1914) by Bithia Mary Crocker

How can I present this data in an easily readable and understandable way?


回答1:


String sBooks = "Books on Chennai:\n ....";

Replace: \n with <br>:

sBooks = sBooks.replace("\n", "<br>");

Example using a TextView:

myTextView.setText(Html.fromHtml(sBooks));



回答2:


You should replace this \n with <br>. For more android HTML tags support go to Android Support HTML Tags or daniel-codes.blogspot.in




回答3:


Just replace \n with <br/> tag and then pass the whole string to Html.fromHtml(String). By this it will be displayed in proper format.




回答4:


Use this:

b.setText(Html.fromHtml("<b>" + your string+ "</b>" + "<br/>" + cursor.getString(1)));



回答5:


The best option is instead of TextView use a WebView and

final String mimeType = "text/html";
    final String encoding = "UTF-8";
    webView.loadDataWithBaseURL("", contentToShowString, mimeType,
            encoding, "");



回答6:


have you tried with \r\n ?`

If this not works then you can try with : \n with escape sequence : \\\n
I hope this will help you :)

Something like this

string = string.replace("\\\n", System.getProperty("line.separator"));


来源:https://stackoverflow.com/questions/21670174/how-to-have-html-new-line-feature-in-android-text-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!