Reference:
Android Combat Skills: use TextView to achieve rich Text---set different font styles in the same TextView
Demo:
PrivateSpannablestringbuilder content =NewSpannablestringbuilder (); Private Static FinalForegroundcolorspan Style_error =NewForegroundcolorspan (color.red); Private Static FinalForegroundcolorspan Style_info =NewForegroundcolorspan (Color.Black); Private intStart = 0; Private intEnd = 0; Public voidAppendlog (String msg,inttype) {TextView LogView=(TextView) Findviewbyid (R.id.logview); Content.append (msg); Start=end; End+=msg.length (); Switch(type) { CaseLog.TYPE_ERROR:content.setSpan (Style_error,start,end, spanned.span_exclusive_exclusive); Break; CaseLog.TYPE_INFO:content.setSpan (Style_info,start,end, spanned.span_exclusive_exclusive); Break; default: Break; } if(logview!=NULL) {logview.settext (content); //Scroll to the end intoffset = Logview.getlinecount () *logview.getlineheight (); if(Offset >logview.getheight ()) {Logview.scrollto (0,offset-logview.getheight ()); } } }
Android implements RichText style in TextView