Android TextView sets the background color and text color of some text, androidtextview
Implemented through SpannableStringBuilder, it is like the <span> element in html that changes the text color or background color of the specified text.
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 in Word.
- RelativeSizeSpan (float proportion) ---- 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, this is more flexible to use. proportion> 1 is zoom in, and 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)
- BackgroundColorSpan (int color) ---- background coloring. The parameter is a Color value. You can directly use the constant defined in android. graphics. Color, or use color. rgb (int, int, int)
- ForegroundColorSpan (int color) ---- foreground color, that is, the color of the word. The parameter is consistent with that of the background color.
- TypefaceSpan (String family) ---- font. The parameter is the font name, such as "sans" and "sans-serif ".
- StyleSpan (Typeface style) ----- font style, such as BOLD and ITALIC. The parameters are constants defined in android. graphics. 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.
Android development: how to set the text and background colors of textView statically and dynamically? Beginners do not know what to fill in details
Static is to fill in the attributes in the visualized Graphical Layout.
TextView1.TextColor (text color)
TextView1.Background (background color)
In @ android, all defined color values are automatically displayed.
For example, @ android: color/holo_blue_bright
Dynamic is set color in the program
Import android. graphics. Color;
TextView1.setTextColor (Color. RED );
TextView1.setBackgroundColor (Color. RED );
How to change the background color when a TextView is clicked?
Add
GetCodeView. setTextColor (Color. RED); // click to modify the Color.
// Restore to the original black
New Handler (). postDelayed (new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
GetCodeView. setTextColor (Color. BLACK );
}
},200 );
/