TextView displays html-style text,
Project requirements:
TextView displays a piece of text in the format of Snow White (name, number of words are not sure) sent to you 2 (number of messages, not sure) Message
The length of the name and number in the text section is uncertain, and the names and numbers must have their own colors.
At first, I wanted to use (convert) SpannableString and SpannableStringBuilder, because it can display different colors for a text segment.
However, it seems that it can only fix the position where the text is displayed and the style is displayed.
Then I thought of using
Html.fromHtml(String str)
.
The method name is simple, that is, the html text corresponding to the string str can be displayed.
For example:
Html. fromHtml (<font color = 'red' size = '24'> Hello </font> ")
You will be displayed in html format, with a red font size of 24
Let's look at the simple use of this method through a small Demo:
I have three strings. The names and numbers in the strings are different in length, so that the names are displayed in red, the numbers are displayed in blue, and other texts are displayed in gray by default.
First write the layout file, three textviews
<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 onCreate () method of the Activity
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById(R.id.html _ text); textView2 = (TextView) vertex _ text2); textView3 = (TextView) findViewById(R.id.html _ text3 ); names = new ArrayList <> (); counts = new ArrayList <> (); message = new ArrayList <> (); names. add ("Altman"); names. add ("Snow White and the Seven Dwarfs"); names. add ("ward day wona moshuai Shuai de bu yaobu Yaode"); counts. add (1); counts. add (123); counts. add (9090909); for (int I = 0; I <3; I ++) {message. add ("<font color = 'red' size = '20'>" + names. get (I) + "</font>" + "sent to you" + "<font color = 'Blue 'size = '30'>" + counts. get (I) + "</font>" + "items");} textView. setText (Html. fromHtml (message. get (0); textView2.setText (Html. fromHtml (message. get (1); textView3.setText (Html. fromHtml (message. get (2 )));}
Check whether it is very simple. As long as html is simple, this effect can be achieved.