一款Android下的名片识别源代码
http:///thread-195259-1-1.html

Android+音乐播放器歌词列表式同步显示
http:///thread-196184-1-1.html

【上海见面会】上海移动开发者大会精彩瞬间
http:///thread-196868-1-1.html

TextView不单单能够显示简单的文本内容,也可以显示复杂的文本内容:富文本


显示富文本有如下几种:
1、显示带标签的文本,但是没有图片内容
带标签的字符串内容无法直接通过TextView的setText直接显示,如果直接显示,则字符串什么内容,就显示什么内容了,而是需要通过Html.fromHtml进行转换成对应格式的字符串样式;因为任何的组件或者组件上面的内容,都是Android通过画笔画上去的;所以,要显示富文本,带各种样式,就有很多种实现的方式了;可以使用WebView组件显示HTML页面,也可以继承View类或者子类,覆盖onDraw方法直接绘制富文本或者图像,扯远了;

android TextView的使用总结_TextViewView Code
                tv = (TextView) findViewById(.textViewHTML01);                   StringBuilder sb = new StringBuilder();                   sb.append("<font color='yellow'>Hello Android</font><br>");                   sb.append("<font color='blue'> <big><i>Hello Android</i></big></font>");                   sb.append("<font color='@" + android.R.color.darker_gray                                   + "'><tt><b><big><u>Hello world</u></big></b></tt></font><br>");                   sb.append("<font color='yellow'><big><u> <a href='http://'>To google webSite</a></u></big></font>");                   CharSequence charSequence = Html.fromHtml(sb.toString());                   tv.setText(charSequence);                   tv.setMovementMethod(LinkMovementMethod.getInstance());

这里需要注意的是:Html.fromHtml转化后,且设置setText;设置完成,必须要进行setMovementMethod;如果没有设置的话,虽然文本的字符串的样式也会显示成功,但是上面的超链接 就没有用了;

android TextView的使用总结_移动开发_03

2、如何显示图像到TextView
有两种方式实现:
第一种:在Html.fromHtml方法里面,实现ImageGetter接口来完成

android TextView的使用总结_移动开发_04View Code
private void getDrawableTextView() {                   String html = "<img src='ic_launcher'/>";                   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;               }

android TextView的使用总结_移动开发_06

第二种方式实现图片在TextView上显示:

android TextView的使用总结_adnroid开发_07View Code
        /**            * 根据资源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

android TextView的使用总结_TextView_09View Code
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类用于描绘绘制的属性,如画笔的颜色,粗细等,所以通过自定义这个类,让文本样式变的更加的灵活;

android TextView的使用总结_adnroid开发_11View Code
        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());           }

android TextView的使用总结_adnroid开发_13

 

原文链接:http:///thread-196191-1-1.html