50 Android Development Tips (10 for TextView add style)

Source: Internet
Author: User

Let 's start with an example of a control:
(Original address: http://blog.csdn.net/vector_yi/article/details/24428085)

You must have seen it many times on a phone like this, but have you ever considered how it is implemented? You might think this is a slightly more complex view of your own definition, but, yes, it's actually just a native textview. TextView is a simple and wonderful widget that you can use to present text in different styles and formats.
Give two simple examples: 1. Adding a hyperlink to TextView is actually very easy, and we can write HTML to TextView: First add a <string/> tag to string.xml in Res/values:
<string name= "Text1" ><! [cdata[Visit <a href=\ "http://blog.csdn.net/vector_yi\" >vector_yi\ ' s blog</a>]]></string>
then simply add the following code to the activity file:
Final TextView TextView1 = (TextView) Findviewbyid (R. Id. my_text_view_html);    TextView1. SetText (Html. fromhtml (GetString (R. String. Text1));    TextView1. Setmovementmethod (Linkmovementmethod. getinstance ());
2. Set the background and foreground color for the specified part of text in TextViewAdd a string to the same string.xml in Res/value:
<string name= "Text2" >hello World, homeactivity!</string>
Next, you can use the Spannablestring class to:
Final spannable text2 = new Spannablestring (        getString (R. String. Text2));    Text2. Setspan (New Backgroundcolorspan (Color. Red), 1, 4, 0);//from index 1 to index 4, set the background color to red    text2. Setspan (New Foregroundcolorspan (color. BLUE), 6, 9, 0);
here is the display effect:

Note: TextView does not support all html tags = =, but it is possible to try out different tags.





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.