Android TextView Display HTML-style text _android

Source: Internet
Author: User

First of all, to say the project needs:

TextView display a paragraph of text, the format is: Snow White (name, words are not sure) sent you 2 (number of messages, indeterminate) message

The length of the name and number in this text is indeterminate and requires that the name and number each have their own color.

Just thought of using

html.fromhtml (String str) to implement.

Look at the method name is very simple, you can display the string str corresponding HTML format text

Like what:

html.fromhtml (<font color= ' red ' size= ' > Hello </font> ')

You'll see it in HTML format, red font size 24

Then look at the simple use of this method with a small demo:

I have three strings, the names in the string, the length of the numbers are different, to achieve the name display red, digital display blue, other text display the default gray effect

First write layout file, three TextView

 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Xmlns:tools = "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin
"Android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @dimen/activity_vertical_margin " android:gravity= "center" android:orientation= "vertical" tools:context= ". Mainactivity "> <textview android:id=" @+id/html_text "android:gravity=" center "android:layout_width=" WRAP_ Content "android:layout_height=" wrap_content "/> <textview android:id=" @+id/html_text2 "android:gravity=" Center "android:layout_width=" wrap_content "android:layout_height=" wrap_content "/> <textview android:id=" @+ Id/html_text3 "android:gravity=" center "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " > </linearlayout> 

Then the activity of the OnCreate () method

@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView = (TextView) Findviewbyid (r.id.html_text);
TextView2 = (TextView) Findviewbyid (R.ID.HTML_TEXT2);
TEXTVIEW3 = (TextView) Findviewbyid (R.ID.HTML_TEXT3);
names = new arraylist<> ();
counts = new arraylist<> ();
message = new arraylist<> ();
Names.add ("Altman");
Names.add ("Snow White with Seven Dwarfs");
Names.add ("Ward Day Warner Mo 帅 Shand Bu yao Bouyaude");
Counts.add (1);
Counts.add (123);
Counts.add (9090909);
for (int i = 0; i < 3; i++) {
message.add ("<font color= ' red ' size= ' >" +names.get (i) + "</font>" + "sent to you "+
" <font color= ' Blue ' size= ' > "+counts.get (i) +" </font> "+" article info ");
}
Textview.settext (html.fromhtml (message.get (0)));
Textview2.settext (Html.fromhtml (Message.get (1)));
Textview3.settext (Html.fromhtml (Message.get (2)));

Look at the effect of the picture, is not very simple, as long as the simple HTML can achieve this effect

The above content is a small series to share the Android TextView display HTML style text, I hope this article can be shared to help everyone.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.