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.