码迷,mamicode.com
首页 > 移动开发 > 详细

32Spannable的使用(Android显示html带图片 (表情开发))

时间:2014-12-31 10:06:34      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

Android中显示html文件要用Html.fromHtml(...)处理过的返回值,返回值可以成为setText()的参数。只显示带文本的html可以用下面的方法处理html文件。

public static Spanned fromHtml (String source)  

显示带图片的html要用下面的方法处理html文件。

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)  
<span style="font-family: Simsun;font-size:14px; text-indent: 2em; background-color: rgb(255, 255, 255);">ImageGetter 为处理html中<img>的处理器,生成Drawable对象并返回。</span>

创建ImageGetter 主要实现下面的方法,source为<img>标签中src属性的值。

public Drawable getDrawable(String source)  

下例为在TextView和EditView中显示html,并插入图片。

下图只显示html文字,点击按钮会在TextView和EditView文本后添加图片。

技术分享

代码:

package com.example.test;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	TextView tv;
	EditText et;
	Button addPic;
	String ct;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et=(EditText) this.findViewById(R.id.editText1);
        
        tv=(TextView) this.findViewById(R.id.tv);
        ct="aaa<font color=\"red\">aaa</font>";
        addPic=(Button) this.findViewById(R.id.AddPic);
        addPic.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ct+="<img src=\""+R.drawable.ic_launcher+"\"/>";
				 refreshView();
			}
        	
        });
       
       refreshView();
        
        
    }
    private void refreshView(){
    	 et.setText(Html.fromHtml(ct,imageGetter,null));
         tv.setText(Html.fromHtml(ct,imageGetter,null));
    }
    ImageGetter imageGetter = new ImageGetter()  
    {  
        @Override  
        public Drawable getDrawable(String source)  
        {  
            int id = Integer.parseInt(source);  
            Drawable d = getResources().getDrawable(id);  
            d.setBounds(0, 0, d.getIntrinsicWidth(), d .getIntrinsicHeight());  
            return d;  
        }  
    };  
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" 
    
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
    <EditText 
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    
    <Button 
        android:id="@+id/AddPic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        />

</LinearLayout>







32Spannable的使用(Android显示html带图片 (表情开发))

标签:

原文地址:http://blog.csdn.net/chenfuduo_loveit/article/details/42290315

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!