1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android textview显示表情 Android开发-TextView中显示QQ表情类的图片和超链接

android textview显示表情 Android开发-TextView中显示QQ表情类的图片和超链接

时间:2022-05-28 16:34:16

相关推荐

android textview显示表情 Android开发-TextView中显示QQ表情类的图片和超链接

1.

布局文件:

/apk/res/android"

xmlns:tools="/tools"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:id="@+id/tv_have_image"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="" />

2. 源代码:

package com.xgh.test.view;

import java.lang.reflect.Field;

import android.app.Activity;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.text.Html;

import android.text.Html.ImageGetter;

import android.text.method.LinkMovementMethod;

import android.text.Spanned;

import android.view.Menu;

import android.widget.TextView;

public class ImageOnTextViewActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.image_on_textview);

TextView tv = (TextView)

findViewById(R.id.tv_have_image);

String htmlString =

"图1

src='face1'/>图2

src='face2'/>图3

href=''>

src='face3'/>

href=''>百度

";

//Spanned继承与CharSequence

Spannedspanned = Html.fromHtml(htmlString, new ImageGetter() {

@Override

public

Drawable getDrawable(String source) {

//getResources()可获取项目下res目录下的所有资源

Drawable

drawable =

getResources().getDrawable(getResourceIdByName(source));

if(source.equals("face3")){

//放在成原来两倍来显示

drawable.setBounds(0, 0,

drawable.getIntrinsicWidth()*2,

drawable.getIntrinsicHeight()*2);

}else{//如果不是face3,则以图片原比例显示,如果不设置Bounds话,则默认不显示,那样会看没见图片,也就是说Bounds的四个参数全为0

drawable.setBounds(0, 0,

drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

}

return

drawable;

}

}, null);

//设置显示的内容

tv.setText((CharSequence)spanned);

//设置超链接有效,如果不设置,则点击第3图片,不会链接到百度

tv.setMovementMethod(LinkMovementMethod.getInstance());

}

public Integer getResourceIdByName(String name)

{

try {

//利用反射

Field field =

R.drawable.class.getField(name);

return

field.getInt(null);

} catch (SecurityException e)

{

// TODO

Auto-generated catch block

e.printStackTrace();

} catch (NoSuchFieldException

e) {

// TODO

Auto-generated catch block

e.printStackTrace();

} catch

(IllegalArgumentExceptione) {

// TODO

Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException

e) {

// TODO

Auto-generated catch block

e.printStackTrace();

}

return null;

}

}

运行结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。