[Android] TextView: Set individual font styles. androidtextview

Source: Internet
Author: User

[Android] TextView: Set individual font styles. androidtextview

1 SpannableString msp = new SpannableString ("test" + XM + "replacing the current number will send a normal text message for verification"); 2 msp. setSpan (new ForegroundColorSpan (Color. BLUE), 2, XM. length () + 2, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );

 

 

In the impression, TextView is a control used to display text. You can use the android: text attribute in the layout file to set realistic characters, or use the findViewById (XXX) of the Context object) after obtaining the TextView object, use the setText () method to dynamically assign values, set the single-line mode through the android: singleLine attribute, set the overall color through android: textColor, and use android: autoLink to set the automatic connection (none,) type. I have never considered displaying text in a variety of ways in the TextView control. TextView further deepen: Textview can format its text. By querying materials, we learned that formatting text is mainly divided into two categories: the first type: HTML tag formatting text code is relatively simple, as shown below:

  

1 import android. app. activity; 2 import android. OS. bundle; 3 import android. text. html; 4 import android. widget. textView; 5 6 public class AndroidFronColorTest extends Activity {7 @ Override 8 public void onCreate (Bundle savedInstanceState) {9 super. onCreate (savedInstanceState); 10 11 setContentView (R. layout. main); 12 13 TextView htmlFormateTextView = (TextView) findViewById (R. id. testTextView); 14 15 String source = "This is just a test, test the format of <u> underline </u>, <I> italic </I>, and <font color = 'red'> red </font> "; 16 17 htmlFormateTextView. setText (Html. fromHtml (source); 18} 19}

 

The second type is formatted using SpannableString.

  

1 public class TextViewLinkActivity extends Activity {2 TextView mTextView = null; 3 SpannableString msp = null; 4 5 6 @ Override 7 public void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. main); 10 11 mTextView = (TextView) findViewById (R. id. myTextView); 12 13 // create a SpannableString object 14 msp = new SpannableString ("font test font size half twice foreground color background color normal bold italic rough Italic Strikethrough x1x2 Phone Mail website text message MMS Map x axis synthesis "); 15 16 // set the font (default, default-bold, monospace, serif, sans-serif) 17 msp. setSpan (new TypefaceSpan ("monospace"), 0, 2, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 18 msp. setSpan (new TypefaceSpan ("serif"), 2, 4, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 19 20 // set the font size (absolute value, unit: pixels) 21 msp. setSpan (new AbsoluteSizeSpan (20), 4, 6, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 22 msp. setSpan (new AbsoluteSize Span (20, true), 6, 8, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // The second parameter boolean dip. If it is true, it indicates that the front font size is in dip; otherwise, it is pixel, same as above. 23 24 // set the font size (relative value, unit: pixel). The parameter indicates how many times the default font size is 25 msp. setSpan (new RelativeSizeSpan (0.5f), 8, 10, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 0.5f indicates half of the default font size 26 msp. setSpan (new RelativeSizeSpan (2.0f), 10, 12, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f indicates two times the default font size 27 28 // sets the font foreground color 29 msp. setSpan (new ForegroundColorSpan (Color. MAGENTA), 12, 15, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // set the foreground color to magenta 30 31 // set the font background color to 32 msp. setSpan (new BackgroundColorSpan (Color. CYAN), 15, 18, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // set the background color to cyan 33 34 // set the font style to normal, bold, italic, and bold italic 35 msp. setSpan (new StyleSpan (android. graphics. typeface. NORMAL), 18, 20, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 36 msp is normal. setSpan (new StyleSpan (android. graphics. typeface. BOLD), 20, 22, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 37 msp in bold. setSpan (new StyleSpan (android. graphics. typeface. ITALIC), 22, 24, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 38 msp in italic. setSpan (new StyleSpan (android. graphics. typeface. BOLD_ITALIC), 24, 27, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // bold italic 39 40 // set the underline 41 msp. setSpan (new UnderlineSpan (), 27, 30, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 42 43 // sets the strikethrough line 44 msp. setSpan (new StrikethroughSpan (), 30, 33, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); 45 46 // you can specify 47. setSpan (new SubscriptSpan (), 34, 35, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // subscript 48 msp. setSpan (new SuperscriptSpan (), 36, 37, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mark 49 50 // hyperlink (additional response to setMovementMethod needs to be added) 51 msp. setSpan (new URLSpan ("tel: 4155551212"), 37, 39, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // call 52 msp. setSpan (new URLSpan ("mailto: webmaster@google.com"), 39, 41, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mail 53 msp. setSpan (new URLSpan ("http://www.baidu.com"), 41, 43, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // network 54 msp. setSpan (new URLSpan ("sms: 4155551212"), 43, 45, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // sms: Or smsto: 55 msp. setSpan (new URLSpan ("mms: 4155551212"), 45, 47, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mms: Or mmsto: 56 msp. setSpan (new URLSpan ("geo: 38.899533,-77.036476"), 47, 49, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // map 57 58 // set the font size (relative value, unit: pixel) parameter to 59 msp of the default font width. setSpan (new ScaleXSpan (2.0f), 49, 51, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f indicates twice the default font width, that is, to enlarge the X axis to twice the default font width, and the height remains unchanged 60 // The SpannableString object is set to TextView 61 myTextView. setText (sp); 62 // set TextView. You can click 63 myTextView. setMovementMethod (LinkMovementMethod. getInstance (); 64} 65}

  

Note the following when using the SpannableString object:
Functions of Spanned. SPAN_EXCLUSIVE_EXCLUSIVE:
It is used to identify whether to apply the new characters before and after the text in the Span range. Spanned. SPAN_EXCLUSIVE_EXCLUSIVE (not included before and after), Spanned. SPAN_INCLUSIVE_EXCLUSIVE (included first, not included later), Spanned. SPAN_EXCLUSIVE_INCLUSIVE (not included earlier, not included later), Spanned. SPAN_INCLUSIVE_INCLUSIVE (both before and after ).

 

 

Note: The Source of the article cannot be found. Please forgive me.


How does android TextView set the color of an individual font and wrap the text?

TextView TV = (TextView) findViewById (R. id. TV); TV. setText (Html. fromHtml ("the most professional Android developer community: juapk.com"); (2) set text wrapping in TextView
TextView TV = (TextView) findViewById (R. id. TV); TV. setText ("the most professional Android developer community: \ njuapk.com"); (3) set individual font colors and wrap text in TextView
TextView TV = (TextView) findViewById (R. id. TV); TV. setText (Html. fromHtml ("the most professional Android developer community: juapk.com "));

The font needs to be set for each TextView in Android development. Can I directly set the font for textview on all pages in one place?

You can customize your own TextView controls. I don't know how to create and use custom controls Baidu's Android custom controls or composite controls.

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.