Android TextView sets the background color and text color of some texts

Source: Internet
Author: User

Android TextView sets the background color and text color of some texts

Implemented through SpannableStringBuilder, which is like changing the text color or background color of a specified text by elements in html.

 

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); String str = "this is a demo of setting the background color and foreground color of TextView! "; Int bstart = str. indexOf ("background"); int bend = bstart + "background ". length (); int fstart = str. indexOf ("foreground"); int fend = fstart + "foreground ". length (); SpannableStringBuilder style = new SpannableStringBuilder (str); style. setSpan (new BackgroundColorSpan (Color. RED), bstart, bend, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); style. setSpan (new ForegroundColorSpan (Color. RED), fstart, fend, Spannable. SPAN_EXCLUSIVE_INCLUSIVE); TextView tvColor = (TextView) findViewById (R. id. TV _color); tvColor. setText (style) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

  • AbsoluteSizeSpan (int size) ---- set the font size. The parameter is an absolute value, which is equivalent to the font size RelativeSizeSpan (float proportion) in Word ---- set the font size. The parameter is a multiple of the default font size, for example, if the default font size is x, the configured font size is x * proportion, which is flexible to use. proportion> 1 is zoom in ), proportion <1 is zoom out)
    ScaleXSpan (float proportion) ---- scale the font. Similar to the preceding one, the default value is 1. After setting it, It is multiplied by proportion. If it is greater than 1, it is zoon in ), zoom out (zoom out) BackgroundColorSpan (int color) ---- background coloring. The parameter is a color value and can be directly used by android. graphics. the constant defined in Color or Color. rgb (int, int, int) ForegroundColorSpan (int color) ---- foreground coloring, that is, the color of the word. The parameter is consistent with the background coloring TypefaceSpan (String family) ---- font, the parameter is the font name, such as "sans", "sans-serif", and other StyleSpan (Typeface style) ----- the font style, such as bold and italic. The parameter is android. graphics. Constants defined in Typeface, such as Typeface. BOLD and Typeface. ITALIC. StrikethroughSpan ---- if this style is set, there will be a line passing through all words from the middle, just like being crossed out.

     

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.