1.显示丰富的文本(URL、字体大小、颜色等)
TextView中预定了一些类似Html的标签,通过标签可以使TextView显示不同的颜色、大小、字体的文字。
- <font> : 设置颜色和字体
- <big>: 设置大号
- <small>: 设置小号
- <i><b>: 斜体、粗体
- <a>: 链接地址
- <img>: 插入图片
使用这些标签可以使用Html.fromHtml方法。
2.将文本中的URL地址、邮箱地址、电话产生超链接的效果
使用android:autoLink来设置,首先在的layout文件夹里面TextView控件增加
android:autoLink="all"
然后在Java中进行设置:
textview.setMovementMethod(LinkMovementMethod.getInstance());
这样展示出现的内容就可以出现超链接的效果。例如,在TextView点击网址链接后,就可以调转到浏览器并打开响应的链接。内容可以是Html格式的内容,也可以是String格式的,在使用Html显示超链接的时候,必须保证添加上协议好,例如:http://。否则在识别网址的时候,不加的话,可能会判断为Activity,然后导致崩溃。使用String格式的内容,在TextView添加setAutoLinkMask(Linkify.ALL)设置之后,可以不加协议识别出来链接,但是推荐还是加上。而这两种方法,都得设置一下setMovementMethod,才会跳转。
3.TextView显示表情图像和文字
如果需要在TextView里面展示res资源里面的图片的话,可以通过编写html内容,并通过Html.fromHtml方法来将内容转换为CharSequence来显示。
显示的核心代码:
String html =
"图像1<image='image1'>"
;
// 其中image1就是图片在drawable的name
Html.fromHtml(html,
new
ImageGetter() {
@Overrride
public
Drawable getDrawable(String source) {
Drawable drawable = getResource.getDrawable(getResourceId(source));
return
drawable;
}
}
public
int
getResourceId(String name) {
try
{
// 根据资源ID的变量名获得Field的对象,使用反射机制来实现的
Field field = me.renhui.drag_sort_listview.R.drawable.
class
.getField(name);
// 取得并返回资源的id的字段(静态变量)的值,使用反射机制
return
Integer.parseInt(field.get(
null
).toString());
}
catch
(Exception e) {
e.printStackTrace();
}
return
0
;
}
4.TextView单击链接触发点击事件(本例事件是弹出Activity也可以替换为其他事件)
![](/qrcode.jpg)
通过对TextView的内容设置span来给TextView设置点击事件,这样的话就能够通过点击点击TextView中的链接来弹出Activity
5.TextView实现跑马灯的效果
基本的实现方式就是在xml设置TextView的属性:
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true" // 不添加这个属性不能成功跑动起来