TextView显示表情图像和文字
2014-03-17 21:01
429 查看
通过TextView显示出表情图像和文字。
一、建立工程,在drawable下放入五张表情图片
View Code
四、效果图
一、建立工程,在drawable下放入五张表情图片
package com.study.android_textview2; import java.lang.reflect.Field; import android.R.drawable; 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) { e.printStackTrace(); } 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<img src='image1'/>图像2<img src='image2'/>图像3<img src='image3'/><p>"; html += "图像4<a href='http://www.baidu.com'><img src='image4'/></a>图像5<img src='image5'/>"; CharSequence charSequence = Html.fromHtml(html, new ImageGetter() { @Override public Drawable getDrawable(String source) { //获得系统资源的信息,比如图片信息 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; } }
View Code
四、效果图
相关文章推荐
- Android开发(16)-TextView显示表情图像和文字
- Android编程开发实现TextView显示表情图像和文字的方法
- Android开发 TextView显示表情图像和文字
- Android开发(16)-TextView显示表情图像和文字
- Android开发(16)-TextView显示表情图像和文字
- iOS swift 实现图文混排 emoji小表情和文字都显示在一个textview中
- Android学习札记48:将TextView中的指定文字转换为表情显示
- Android的TextView实现文字高亮显示和显示表情
- android 在textView或者EditText当中显示文字加表情符号
- 在TextView中显示表情图像和文本
- android textview 显示表情和文字 表情带超链接
- TextView 显示图像+文字的方法
- android中textview的文字处理--同一段文字部分文字作高亮或者显示不同大小等
- TextView过长显示省略号, TextView文字中间加横线
- Android之TextView、EditText控件显示表情图片
- android 文字图片混排,在一个TextView中显示文字穿插图片,文字变色效果
- 在一个TextView之中,显示多种颜色字号的文字
- TextView显示丰富多彩的文字(四)——从源码角度解释格式化显示的原理
- 在TextView中显示文字和图片
- Android TextView显示表情、标签、超链接