显示富文本有如下几种:
1、显示带标签的文本,但是没有图片内容
带标签的字符串内容无法直接通过TextView的setText直接显示,如果直接显示,则字符串什么内容,就显示什么内容了,而是需要通过Html.fromHtml进行转换成对应格式的字符串样式;因为任何的组件或者组件上面的内容,都是Android通过画笔画上去的;所以,要显示富文本,带各种样式,就有很多种实现的方式了;可以使用WebView组件显示HTML页面,也可以继承View类或者子类,覆盖onDraw方法直接绘制富文本或者图像,扯远了;
tv = (TextView) findViewById(R.id.textViewHTML01); 
                StringBuilder sb = new StringBuilder(); 
                sb.append("Hello Android
"); 
                sb.append(" Hello Android"); 
                sb.append("Hello world
"); 
                sb.append(" To google webSite"); 
                CharSequence charSequence = Html.fromHtml(sb.toString()); 
                tv.setText(charSequence); 
                tv.setMovementMethod(LinkMovementMethod.getInstance());
这里需要注意的是:Html.fromHtml转化后,且设置setText;设置完成,必须要进行setMovementMethod;如果没有设置的话,虽然文本的字符串的样式也会显示成功,但是上面的超链接 就没有用了;

2、如何显示图像到TextView
有两种方式实现:
第一种:在Html.fromHtml方法里面,实现ImageGetter接口来完成
private void getDrawableTextView() { 
                String html = ""; 
                CharSequence charSequence = Html.fromHtml(html, new ImageGetter() { 
  
                        @Override
                        public Drawable getDrawable(String source) { 
                                // TODO Auto-generated method stub 
                                Drawable drawable = getResources().getDrawable( 
                                                getResourceId(source)); 
                                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 
                                                drawable.getIntrinsicHeight()); 
  
                                return drawable; 
                        } 
                }, null); 
  
                tv.setText(charSequence); 
                tv.setMovementMethod(LinkMovementMethod.getInstance()); 
        } 
  
        /** 
         * 由于无法直接使用文件名来引用图像资源,利用反射技术获得图像资源ID 
         *  
         * @param name 
         * @return 
         */
        private int getResourceId(String imageName) { 
                int resId = 0; 
                try { 
                        Field field = R.drawable.class.getField(imageName ); 
resId   = Integer.parseInt(field.get(null).toString()); 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                } 
                return id; 
  
        }

第二种方式实现图片在TextView上显示:
 /** 
         * 根据资源ID获得资源图像的Bitmap对象,然后由该对象创建ImagaSpan对象 
         * 创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像 然后用ImageSpan对象替换ic_launche 
         * 将图像显示在TextView上面 
         */
  
        private void showBitmapToTextView() { 
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
                                R.drawable.ic_launcher); 
                ImageSpan imageSpan = new ImageSpan(this, bitmap); 
                SpannableString spannableString = new SpannableString("ic_launche"); 
                spannableString.setSpan(imageSpan, 0, 10, 
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
                tv.setText(spannableString); 
                tv.setMovementMethod(LinkMovementMethod.getInstance()); 
        }
3、同时设置文本的颜色和文本的背景色
    
因为BackgroundColorSpan只能设置文字的背景色,为了也能设置文本的颜色,可以自定义一个ColorSpan继承CharacterStyle
class ColorSpan extends CharacterStyle { private int mTextColor; private int mBackgroundColor; public ColorSpan(int mTextColor, int mBackgroundColor) { // TODO Auto-generated constructor stub this.mBackgroundColor = mBackgroundColor; this.mTextColor = mTextColor; } /** * 这个方法是在系统开始绘制要设置样式的字符串之前调用,以便修改文字的属性 */ @Override public void updateDrawState(TextPaint tp) { tp.setColor(mTextColor); tp.bgColor = mBackgroundColor; } }
这种方式来设置,因为TextPaint是Paint的子类,Paint类用于描绘绘制的属性,如画笔的颜色,粗细等,所以通过自定义这个类,让文本样式变的更加的灵活;
 private void setTextColorAndBackColor() { 
                String text ="<没有背景><黄色背景>\n\n<蓝色背景,红色文字>"; 
                SpannableString spannableString = new SpannableString(text); 
                BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW); 
                int start = 6; 
                int end = 12; 
                spannableString .setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
                start=14; 
                ColorSpan  colorSpan = new ColorSpan(Color.RED, Color.BLUE); 
                spannableString.setSpan(colorSpan, start, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
                tv.setText(spannableString); 
                tv.setMovementMethod(LinkMovementMethod.getInstance()); 
        }

- 1楼 cheap cartier womens watch replicas 发表于 2015-3-30 19:22:31
- A empresa registrou receita do segundo trimestre para o per cheap cartier womens watch replicas http://www.yikaodian.cn/forum.php?mod=viewthread&tid=103094&fromuid=13650
- 2楼 ca88亚洲城 发表于 2016-9-20 4:04:51
- 写得真是太好了,我要保留上去当作条记,随时查看。 ca88亚洲城 http://yjsc.hbust.com.cn






