zoukankan      html  css  js  c++  java
  • android TextView的使用总结

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

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

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

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


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

    View Code
                    tv = (TextView) findViewById(R.id.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://www.google.com'>To google webSite</a></u></big></font>"); 
                    CharSequence charSequence = Html.fromHtml(sb.toString()); 
                    tv.setText(charSequence); 
                    tv.setMovementMethod(LinkMovementMethod.getInstance());

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

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

    View 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; 
      
            }

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

    View 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

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

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

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

  • 相关阅读:
    笔记56 Mybatis快速入门(七)
    笔记55 Mybatis快速入门(六)
    笔记54 Mybatis快速入门(五)
    笔记53 Mybatis快速入门(四)
    笔记52 Mybatis快速入门(三)
    笔记51 Mybatis快速入门(二)
    笔记50 Mybatis快速入门(一)
    笔记49 在Spittr应用中整合Hibernate
    198. House Robber,213. House Robber II
    303. Range Sum Query
  • 原文地址:https://www.cnblogs.com/vus520/p/2665098.html
Copyright © 2011-2022 走看看