TextView实现图文混合编排的方法

时间:2019-03-31
本文章向大家介绍TextView实现图文混合编排的方法,主要包括TextView实现图文混合编排的方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、简介

在这里实现图文混合编排使用的是:TextView中预定义的类似Html的标签

二、方法

* 1、设置好html标签的文本

String html="<font>图片1</font><img src='image1'/>";
html+="<font>图片2</font><img src='image2'/>";
html+="<font>图片3</font><img src='image3'/>";
html+="<font>图片4</font><img src='image4'/>";
html+="<font>图片5</font><img src='image5'/>"; 

* 2、为之前的文本声明Html.fromHtml,方便TextView解析为html标签

tv_one.setText(Html.fromHtml(text1));

因为有图片,我们要获取图片源,所以上面的那句不行;

所以如下:

CharSequence text=Html.fromHtml(html, new ImageGetter() {中间省略}, null);

new ImageGetter() {中间省略}这部分比较复杂,看实例代码吧,实质就是取到R文件中图片对应的ID 

* 3、将CharSequence字符串序列的文本text插入到TextView控件中即可

tv_textAndImage.setText(text);

这里是charSequence是因为Html.fromHtml方法的返回值是Spanned类型,

看下下面的类图特别好懂:

三、代码实例

效果图

代码

 fry.ActivityDemo2

package fry;

import java.lang.reflect.Field;

import com.example.textViewDemo1.R;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;

public class ActivityDemo2 extends Activity{
  private TextView tv_textAndImage;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity02);
    setTitle("TextViewDemo2");
    tv_textAndImage=(TextView) findViewById(R.id.tv_textAndImage);
    //第一步,设置文本
    String html="<font>图片1</font><img src='image1'/>";
    html+="<font>图片2</font><img src='image2'/>";
    html+="<font>图片3</font><img src='image3'/>";
    html+="<font>图片4</font><img src='image4'/>";
    html+="<font>图片5</font><img src='image5'/>";
    //第二步,告诉TextView控件这是html,并且获取文本中的图片源
    CharSequence text=Html.fromHtml(html, new ImageGetter() {
      
      public Drawable getDrawable(String source) {
        // TODO Auto-generated method stub
        //根据图片资源ID获取图片
        //getResources就是去找项目里面的res文件夹
        Drawable drawable=getResources().getDrawable(getDrawableResurceID(source));
        //一定要加上边界这部分代码。要不然drawable会因为信息不完整读不出来图片
        //分别是left top width height 
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        return drawable;
      }
    }, null);
    //第三步、将CharSequence字符串序列的文本text插入到TextView控件中即可
    tv_textAndImage.setText(text);
    
  }
  /**
   * 获取图片的资源ID
   * @param imageName 图片的名称
   * @return 图片对应的ID
   * 
   */
  private int getDrawableResurceID(String imageName){
    //利用反射机制取得图片的id
    /*
     * 其实是找com.example.textViewDemo1.R.drawable.image1的值,也就是
     * public static final int image1=0x7f020001;
     * 也就是0x7f020001
     * 例如image1,返回的就是0x7f020001
     */
    try {
      Field field=R.drawable.class.getField(imageName);
      return Integer.parseInt(field.get(null).toString()); 
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    return 0;
    
  }
}

/textViewDemo1/res/layout/activity02.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <TextView 
    android:id="@+id/tv_textAndImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。