您的位置:首页 > 移动开发 > Android开发

Android在TextView中实现RichText风格

2016-06-25 16:25 399 查看
参考:

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());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: