Android Learning output HTML format text method detailed

Source: Internet
Author: User
Tags cdata

in the Android Development , we often use textview this control, in the process of use, we use the most method is setText (), pure use basically can meet our daily needs, but sometimes we need to do some processing of words, such as bold, Italic, underline, and so on, these are also available through the native method provided by Android . But what if we need to achieve the following effect:

Text parsing under HTML ( fast version )

As we can see from the picture, there are two sizes, three colors, of this text. We can certainly define multiple TextView to achieve this effect, but if our text changes a lot, it's also very troublesome to define multiple TextView . Then let's introduce a simpler approach:

String str = "Congratulations!" Your phone runs into <font color= ' #F50057 ' ><big><big><big>888888 points </big></big ></big></font>has surpassed the national <font color= ' #00E676 ' ><big><big><big>99% </big></big></big></font> Android phone. ";

Tv.settext (html.fromhtml (str));

In the above code, we used a method html.fromhtml (), which is a method specifically used in Android to parse HTML format, we can put any HTML The code in the format is parsed by this method, and finally we get the results we need.

Using the above method, we can implement various HTML effects in SetText (). This should be able to achieve the needs of 98% in our development, then what are the 1-2% requirements?

Text parsing under HTML ( Multi-language support )

For example, our current app needs to be compatible with multiple languages, including Chinese, French, English, Japanese, and so on. In the usual development process, we all know that the implementation of multiple languages can be achieved by different values folder under the string.xml file. Let's take a look at how we can define HTML -formatted text in the string.xml file, using Chinese as an example.

First, let's try to put the string from the str we defined above directly into the string.xml .

<string name= "Test_string" >

Congratulations to you! Your phone runs into <font color= ' #F50057 ' ><big><big><big>888888 points </big></big></big ></font>

, has surpassed the national <font color= ' #00E676 ' ><big><big><big>99%</big></big></big>< /FONT> 's Android phone.

</string>

Tv.settext (html.fromhtml (getString (r.string.test_string)));

Run the program with the following results:

Without the effect we expected, the HTML format was not parsed. What is the reason for this? The reason is that we use the getString () method, the official interpretation of this method is as follows:

Return the string value associated with a particular resource ID. It'll be stripped of any styled text information.

Returns a string value associated with a specific resource ID that removes text information from any style.

So that means our HTML format is deleted by the getString () method, so our text will become a normal string. To solve this problem, we need to use the strings in our string.xml <![ Cdata[...your HTML ...] > wrap up, the characters that are wrapped by CDATA are ignored by the parser, which means that the HTML formatting in CDATA will be preserved intact and eventually handed over to our Html.fromhtml () to parse. So let's try the following to change the string in our String.xml to the following:

<string name= "Test_string" >

<! [cdata[

Congratulations to you! Your phone runs into <font color= ' #F50057 ' ><big><big><big>888888 points </big></big></big ></font>

, has surpassed the national <font color= ' #00E676 ' ><big><big><big>99%</big></big></big>< /FONT> 's Android phone.

]]>

</string>

Compile run, result compile times wrong:

Error: (5) apostrophe not preceded by \ \ (in weight 枩 town dev Town of Crypto gallium phone manufactoring confirming led 垎 Juan ? <font color= ' #F50057 ' ><BIG><BIG>&L t;big>888888 arrival ?</big></big></big></font>

The key words of the error are:

Apostrophe not preceded by \ \

There is no front of the apostrophe, \

We can tell from the above error message that this is because ' there is no escaping, then I will escape all the single quotes based on the error message. The final results are as follows:

<string name= "Test_string" >

<! [cdata[

Congratulations to you! Your phone runs into <font color=\\ ' #F50057 \ \ ' ><big><big><big>888888 points </big></big>< /big></font>

, has surpassed the national <font color=\\ ' #00E676 \ \ ' ><big><big><big>99%</big></big></big> </font> Android phone.

]]>

</string>

Finally we run again and get the same results as we did at the beginning of the article.

About text parsing in the HTML format of Android that's probably the case, if you have other methods, welcome to the exchange.

Postscript

Finally attached to a trial in the process of errors encountered, originally wanted to reproduce the situation, the results of how can not meet the error. If you have a friend who has encountered this error, you can try the following workaround.

Error:

Multiple annotations found at the line:

Error:multiple substitutions specified in non-positional format; Did you mean to add

The formatted= "false" attribute?

Error:unexpected end Tag String

Possible causes and workarounds for errors:

1. the string in the String.xml has a % , is escaped to the%, that is changed to \\% , or the use of two percent sign for one, that is, to percent .

2. the string in the String.xml is formatted, causing the parser to parse the error, the solution is <string name= "test_string" formatted= "false" > , will Formatted is set to false .

Android Learning output HTML format text method detailed

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.