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对象,除了可以显示图片之外,还可以显示其他丰富的信息。

  • 相关阅读:
    [daily][troubleshoot][archlinux][wps][font] wps文档中的图内容无法显示中文
    [troubleshoot][daily][archlinux][pacman] pacman 与 pip 包文件冲突
    [daily] 宇宙终极shell之zsh
    [knowledge][basic][hardware] 内存的硬件结构(转)
    [troubleshoot][archlinux][X] GPU HANG
    [daily]使用rdtsc指令,测量程序的运行速度 [转]
    [have_fun] 好玩哒小游戏又来啦
    [dpdk] 读开发指南(2)(内容长期整理中)
    [Virtualization][SDN] 讲的很好的SDN软件定义网络视频课程
    [Virtualization][SDN] VXLAN到底是什么 [转]
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/2983423.html
Copyright © 2011-2022 走看看