Android Project Combat (14): TextView display HTML-style text

Source: Internet
Author: User

Original: Android Project Combat (14): TextView display HTML-style text

Project requirements:

TextView displays a piece of text in the form: Snow White (name, word count) sent to you 2(number of messages, indeterminate) message

The length of the name and number in this text is indeterminate, and the name and number are required to have their own color.

At first I thought of using (turn) spannablestring and Spannablestringbuilder to achieve, because it can achieve a text to display a different color

But it seems that it can only fix where the text shows what style, and then give up.

And then I thought about using

html.fromhtml (String str)

To achieve.

Look at the method name is simple, it is possible to display the string str corresponding HTML-formatted text

Like what:

Html.fromhtml (<font color='red' size=' > Hello </ Font>"  )

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

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

I have three strings, the names in the string, the length of the numbers are different, the implementation lets the name display red, the number shows the blue, the other text shows the default gray effect

Write a layout file first, 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's OnCreate () method

@Overrideprotected voidonCreate (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=NewArraylist<>(); Counts=NewArraylist<>(); Message=NewArraylist<>(); Names.add ("Ultraman"); Names.add ("Snow White and Seven Dwarfs"); Names.add ("Ward Day Warner Mo 帅 Shand Bu yiu Bouyaude"); Counts.add (1); Counts.add (123); Counts.add (9090909);  for(inti =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 Information"); } textview.settext (html.fromhtml (message.Get(0))); Textview2.settext (html.fromhtml (message.Get(1))); Textview3.settext (html.fromhtml (message.Get(2))); }

Look, is not very simple, as long as the simple HTML will be able to achieve this effect

Android Project Combat (14): TextView display HTML-style text

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.