码迷,mamicode.com
首页 > 移动开发 > 详细

Android在TextView中实现RichText风格

时间:2016-06-25 17:53:04      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

参考:

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());
            }
        }
    }

 

Android在TextView中实现RichText风格

标签:

原文地址:http://www.cnblogs.com/tt2015-sz/p/5613052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!