参考:
Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
Demo:
private SpannableStringBuilder content = new SpannableStringBuilder();
private static final ForegroundColorSpan STYLE_ERROR = new ForegroundColorSpan(Color.RED);
private static final ForegroundColorSpan STYLE_INFO = new ForegroundColorSpan(Color.BLACK);
private int start = 0;
private int end = 0; public void appendLog(String msg,int type){
TextView logView = (TextView)findViewById(R.id.logView); content.append(msg);
start = end;
end += msg.length();
switch (type){
case Log.TYPE_ERROR:
content.setSpan(STYLE_ERROR,start,end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case Log.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
int offset = logView.getLineCount() * logView.getLineHeight();
if(offset > logView.getHeight()){
logView.scrollTo(0,offset - logView.getHeight());
}
}
}