zoukankan      html  css  js  c++  java
  • android textview组件显示富文本信息

    android 中textview显示富文本信息具有以下几种方式:

    1,利用富文本标签,类似于html标签,如<b>,<font>,<img>等,不过不能直接作为textview.setText的参数值,而应该静html.fromHtml方法将这些文本转换为charsequence对象。如果想要显示图片的时候,还需要实现imagegetter接口

    2,重写ondraw方法

    3,利用webview组件显示html页面

    4,textview中显示图片还可以使用imagespan对象,该对象用来封装bitmap对象,并通过spannableString对象封装imagespan对象,将其作为settext方法的参数。

    方法1的代码如下:

    TextView tv = (TextView) this.findViewById(R.id.tv);
            String html="<strong>我的测试</strong><img src=\"ic_launcher-web.png\"><img src=\"\">";
            CharSequence charSequence=Html.fromHtml(html,new ImageGetter(){
                
                @Override
                public Drawable getDrawable(String arg0) {
                     Drawable drawable=MainActivity.this.getResources().getDrawable(R.drawable.ic_launcher);
                     //下面这句话不可缺少
                     drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                    return drawable;
                }},null);
        tv.setText(charSequence);    
        }

    其中碰到img标签返回的drawable对象是由接口返回的值来决定,如果得到的是网络上的图像,那么显示的就是网络的图像。

        TextView tv = (TextView) this.findViewById(R.id.tv);
            String html="<strong>我的测试</strong><img src=\"http://tp1.sinaimg.cn/2668435432/180/5636292734/0\">";
            CharSequence charSequence=Html.fromHtml(html,new ImageGetter(){
        
                @Override
                public Drawable getDrawable(String arg0) {
                       Drawable d = null;
                       try {
                           InputStream is = new DefaultHttpClient().execute(new HttpGet(arg0)).getEntity().getContent();
                           Bitmap bm = BitmapFactory.decodeStream(is);
                           d = new BitmapDrawable(bm);
                           d.setBounds(0, 0, 200, 300);
    
                       } catch (Exception e) {e.printStackTrace();}
    
                       return d;
                  }
                },null);
        tv.setText(charSequence);    

    利用这种方法更多的是显示从网络上获取的照片(另外一种更广泛的方法是利用webview);如果需要显示的是本地资源文件的图像资源,更多的利用imagespan。

    TextView tv = (TextView) this.findViewById(R.id.tv);
            Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher);
            drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
            ImageSpan span=new ImageSpan(drawable);
            SpannableString spannableString=new SpannableString("span");
            spannableString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(spannableString);
                //用超链接标记文本
                spannableString.setSpan(new URLSpan("tel:4155551212"), 2, 3,  
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
               
         
        }

    可见利用span对象,除了可以显示图片之外,还可以显示其他丰富的信息。

  • 相关阅读:
    关于Linux内核版本
    什么是操作系统?
    进程三种基本状态
    Repo实践指南
    在TortoiseSVN/TortoiseGit中使用BeyondCompare进行差异对比
    Redis安装
    Outlook配置163邮箱
    Nginx安装
    MySQL字符串操作函数
    Java各种反射性能对比
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/2983423.html
Copyright © 2011-2022 走看看