TextView可以添加一些html字段
String html = "<fontcolor='red'>android</font><br>";
// color值可以用文本表示,也可以用十六进制值的文本颜色也可以用RGB表示(比如 "rgb(255,0,0)"。
// 属性值用单引号
html += "<fontcolor='#0000ff'><big><i>I love android</i></big></font><p>";
// <big>表示粗体 <i> 表示斜体
html += "<big><ahref='http://www.baidu.com'>百度</a></big>";
CharSequence charSequence = Html.fromHtml(html);
tv1.setText(charSequence);
// 点击的时候产生超链接。
tv1.setMovementMethod(LinkMovementMethod.getInstance());
此方法一定要在layout中为Textveiw设置android:autoLink=all;
String text = "URLhttp://www.sina.com\n";
text += "email:sanlan@163.com\n";
text += "电话 :159 0064 0525";
tv2.setText(text);
tv2.setMovementMethod(LinkMovementMethod.getInstance());
2 。利用反射得到工程中的资源
public intgetResourceId(String name) {
try {
// 根据资源的ID变量名来获得filed对像,使用反射机制(Field是反射reflect中的)
Field field = R.drawable.class.getField(name);
// 取得并返回资源ID字段(静态变量)的值,使用反射机制
return Integer.parseInt(field.get(null).toString());
} catch(Exception e) {
}
return 0;
}
让图片产生超连接
String html = "图片1<img src='ic_launcher'/>图片2<img src='label_6'/>图片3<img src='label_7'/><p>";
html += "图片4<a href='http://www.baidu.com'><imgsrc='ic_launcher'></a>";
CharSequence charSequence = Html.fromHtml(html,new ImageGetter() {
public DrawablegetDrawable(String source) {
Drawable drawable =getResources().getDrawable(getResourceId(source));
drawable.setBounds(0,0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
}, null);//这里一定要设置边框(setBound),否则图片无法显示。
tv.setText(charSequence);
tv.setMovementMethod(LinkMovementMethod.getInstance());
3.给TextView设置跑马灯效果。TextView中几个关键属性
android:singleLine="true"
android:ellipsize="marquee" 跑马灯效果
android:marqueeRepeatLimit="marquee_forever"重复
android:focusable="true"
android:focusableInTouchMode="true"
4,getField获得类中指定的public属性(也叫字段);getDeclaredField返回指定类中指定的属性(任何可见性)