Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

android - 如何在TextView中显示HTML?(How to display HTML in TextView?)

I have simple HTML :

(我有简单的HTML :)

<h2>Title</h2><br>
<p>description here</p>

I want to display HTML styled text it in TextView .

(我想在TextView显示HTML样式的文本。)

How to do this?

(这该怎么做?)

  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use Html.fromHtml() to use HTML in your XML Strings.

(您需要使用Html.fromHtml()在XML字符串中使用HTML。)

Simply referencing a String with HTML in your layout XML will not work.

(简单地在布局XML中引用带有HTML的String将不起作用。)

This is what you should do:

(这是你应该做的:)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
} else { 
    textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...