android textview表情,Android开发(16)-TextView显示表情图像和文字

从这个案例中我们可以学到当我们美化图片美化界面的时候可以在某一区域输入图片和文字混搭信息,第三张图片按比例缩小,第四张图像有超链接

aa0b4bce675c0f2c8ac30ed119d9b252.png

布局文件

MainActivity.java

package com.example.textview3;

import java.lang.reflect.Field;

import android.os.Bundle;

import android.app.Activity;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.text.Html;

import android.text.Html.ImageGetter;

import android.text.method.LinkMovementMethod;

import android.view.Menu;

import android.widget.TextView;

public class MainActivity extends Activity {

public int getResourceId(String name) {

try {

// 根据资源的ID的变量名获得Field的对象,使用反射机制来实现的

Field field = R.drawable.class.getField(name);

// 取得并返回资源的id的字段(静态变量)的值,使用反射机制

return Integer.parseInt(field.get(null).toString());

} catch (Exception e) {

// TODO: handle exception

}

return 0;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView textView = (TextView) this.findViewById(R.id.textview);

textView.setTextColor(Color.BLACK);

textView.setBackgroundColor(Color.WHITE);

textView.setTextSize(20);// 设置字体的大小

String html = "图像1

image1图像2

image2图像3

image3

";

html += "图像4image4图像5

image5";

CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {

@Override

public Drawable getDrawable(String source) {

// TODO Auto-generated method stub

// 获得系统资源的信息,比如图片信息

Drawable drawable = getResources().getDrawable(

getResourceId(source));

// 第三个图片文件按照50%的比例进行压缩

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

drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2,

drawable.getIntrinsicHeight() / 2);

} else {

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight());

}

return drawable;

}

}, null);

textView.setText(charSequence);

textView.setMovementMethod(LinkMovementMethod.getInstance());

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值