Android TextView中添加图片的方法
2017-08-10 11:43
459 查看
1.重写TextView的onDraw方法(此方法不推荐,除非有特别要求)
2.用TextView支持部分Html,直接用api赋图片
String html = "<img src='" + R.drawable.pictrue+ "'/>";
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
int id = Integer.parseInt(source);
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
CharSequence mCharSequence = Html.fromHtml(html, imageGetter , null);
textView1.setText(charSequence);
textView1.append("图片 ");
3.用ImageSpan和SpannableString
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.picture);
ImageSpan imgSpan = new ImageSpan(this, b);
SpannableString spanString = new SpannableString("icon");
spanString.setSpan(imgSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView2.setText(spanString);
textView2.append("你好");
4. xml中 android:drawableRight="@drawable/button_nav_down"
可以在代码中动态修改
setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)
Drawable可以通过 Drawable mDrawable=getResources().getDrawable(R.drawable.button_nav_up);得 到
但是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法
mDrawable=getResources.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
总代码是这样的:
Drawable mDrawable=getResources().getDrawable(R.drawable.picture);
nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
textview1.setCompoundDrawables(null, null, nav_up, null);
2.用TextView支持部分Html,直接用api赋图片
String html = "<img src='" + R.drawable.pictrue+ "'/>";
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
int id = Integer.parseInt(source);
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
CharSequence mCharSequence = Html.fromHtml(html, imageGetter , null);
textView1.setText(charSequence);
textView1.append("图片 ");
3.用ImageSpan和SpannableString
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.picture);
ImageSpan imgSpan = new ImageSpan(this, b);
SpannableString spanString = new SpannableString("icon");
spanString.setSpan(imgSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView2.setText(spanString);
textView2.append("你好");
4. xml中 android:drawableRight="@drawable/button_nav_down"
可以在代码中动态修改
setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)
Drawable可以通过 Drawable mDrawable=getResources().getDrawable(R.drawable.button_nav_up);得 到
但是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法
mDrawable=getResources.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
总代码是这样的:
Drawable mDrawable=getResources().getDrawable(R.drawable.picture);
nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
textview1.setCompoundDrawables(null, null, nav_up, null);
相关文章推荐
- Android开发:TextView添加超链接的简便方法
- 我的Android进阶之旅------> Android在TextView中显示图片方法
- Android - TextView添加图片
- Android学习之TextView显示html图片的方法
- Android--EditText/TextView 中添加图片,以及点击图片.
- android Button,textView 等控件设置控件内嵌周围图片使用方法
- Android TextView控件文字添加下划线的实现方法
- android Button,textView 等控件设置控件内嵌周围图片使用方法
- Android将TextView中的文字替换为一个小图片的方法
- 我的Android笔记(九)—— TextView显示html图片的方法
- Android实现TextView显示HTML加图片的方法
- android之在EditText、TextView中添加表情图片
- Android中TextView显示插入的图片实现方法
- Android TextView里直接显示图片的三种方法
- Android开发:TextView添加超链接的简便方法
- Android: TextView添加图片(drawable)及动画实例
- android Button,textView 等控件设置控件内嵌周围图片使用方法
- Android TextView显示图片的三种方法
- Android textView文字添加图片 imageSpan使用
- TextView图文混排,显示添加的图片,三种常用方法,亲测