android-----使用ImageSpan对象在TextView组件中显示图像
2015-12-16 16:24
721 查看
如何在TextView中显示图像?
只要用到ImageSpan就非常简单,下面用具体的例子说明一下ImageSpan的用法。
1、随便在布局文件上添加一个Textview对象,如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是一個圖片" />
</RelativeLayout>
2、在Activity中添加如下的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView) findViewById(R.id.tv);
//根据id获取资源图像的bitmap对象
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//根据bitmap对象创建imageSpan对象
ImageSpan imageSpan=new ImageSpan(this, bitmap);
//创建SpannableString对象,以便插入imageSpan对象封装的图像
SpannableString spannableString=new SpannableString("icon 圖片");
//用imageSpan对象替换icon
spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将图像显示在TextView上
tv.setText(spannableString);
}
3、显示效果如下
大功告成,是不是很简单?
只要用到ImageSpan就非常简单,下面用具体的例子说明一下ImageSpan的用法。
1、随便在布局文件上添加一个Textview对象,如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是一個圖片" />
</RelativeLayout>
2、在Activity中添加如下的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView) findViewById(R.id.tv);
//根据id获取资源图像的bitmap对象
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//根据bitmap对象创建imageSpan对象
ImageSpan imageSpan=new ImageSpan(this, bitmap);
//创建SpannableString对象,以便插入imageSpan对象封装的图像
SpannableString spannableString=new SpannableString("icon 圖片");
//用imageSpan对象替换icon
spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将图像显示在TextView上
tv.setText(spannableString);
}
3、显示效果如下
大功告成,是不是很简单?
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories