Android学习笔记2 TextView图文混排,显示添加的图片,三种常用方法,亲测
2013-08-31 13:43
417 查看
图文混排,文字就不说了,主要是显示图片的方法
1、TextView使用ImageSpan显示图片
2.使用Html类进行添加,可达到如图效果,本方法主要是使用html标签的操作,所有内容可以很丰富,其他用途可自查sdk文档
完整源码:TextView图文混排
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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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) {
// 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());
Html类可以将一些常用的html标签进行格式化显示
3、在TextView四周显示图片
1、TextView使用ImageSpan显示图片
ImageSpan span = new ImageSpan(this, R.drawable.ic_launcher);//加载图片的资源 SpannableString spanStr = new SpannableString("http://orgcent.com");//不想要这个字符串可以试试空字符串 spanStr.setSpan(span, spanStr.length()-1, spanStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); mTVText.setText(spanStr);//完成添加
2.使用Html类进行添加,可达到如图效果,本方法主要是使用html标签的操作,所有内容可以很丰富,其他用途可自查sdk文档
完整源码:TextView图文混排
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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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) {
// 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());
Html类可以将一些常用的html标签进行格式化显示
3、在TextView四周显示图片
mTVText.setText("setCompoundDrawables");//添加文字 Drawable d = getResources().getDrawable(R.drawable.ic_launcher);//获取图片资源 d.setBounds(0, 0, 50, 50); //必须设置图片大小,否则不显示 mTVText.setCompoundDrawables(d , null, null, null);设置图片显示的位置(上下左右),此方法的不同之处
相关文章推荐
- TextView图文混排,显示添加的图片,三种常用方法,亲测
- Android 显示 Gif动态图片的三种常用方法
- Android TextView里直接显示图片的三种方法
- ImageUtil常用的显示图片方法,好东西留着别忘记了
- MFC添加背景图片三种方法
- Android中常用的三种图片压缩方法
- Android TextView里直接显示图片的三种方法
- 在MFC对话框中显示图片的三种方法(有两种使用OpenCv)
- Android TextView里直接显示图片的三种方法
- VS2008中添加程序启动时显示图片功能方法
- Swinghacks——显示图片的2种常用方法
- 图片处理的常用三种方法
- 关于Glide为图片添加签名的signature方法,EaseUI头像显示优化
- 在适配器的构造方法添加假数据,然后请求数据后listview等的图片显示不完整,刷新之后才显示的问题。
- jQuery控制TR显示隐藏的三种常用方法
- 正确的添加图片方法 - 解决UIImageView.image = UIImage(named: String)不显示图像的问题
- 为图片添加内阴影效果的三种方法
- Android TextView里直接显示图片的三种方法
- 背景图片居中显示三种方法
- VS2008中添加程序启动时显示图片功能方法