标签:
<resources> <string name="hello"><u>phone:0123456</u></string> <string name="app_name">MyLink</string> </resources>
直接让TextView引用字符串资源的name即可。
2、TextView设置autoLink属性
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:text="@string/link_text_auto" />
如果是代码里:
1、使用Html.fromHtml()
TextView textView = (TextView)findViewById(R.id.tv_test); textView.setText(Html.fromHtml("<u>"+"0123456"+"</u>"));
2、使用TextView的Paint的属性
tvTest.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线
tvTest.getPaint().setAntiAlias(true);//抗锯齿
3、使用SpannableString类
SpannableString content = new SpannableString(str);
content.setSpan(new UnderLineSpan, 0, str.length(), 0);
代码里面自定义超链接样式:
TextView tv=new TextView(this);
tv.setText(Html.fromHtml("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">自定义的超链接样式</a>"));
// 在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象
tv.setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = tv.getText();
if (text instanceof Spannable){
int end = text.length();
Spannable sp = (Spannable)tv.getText();
URLSpan[] urls = sp.getSpans( 0 , end, URLSpan.class );
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans(); // should clear old spans
for (URLSpan url : urls){
URLSpan myURLSpan= new URLSpan(url.getURL());
style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED), sp.getSpanStart(url),
sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//设置前景色为红色
}
tv.setText(style);
}
另外一篇文章中有几个具体的实例可以参考:
http://hunankeda110.iteye.com/blog/1420470
标签:
原文地址:http://www.cnblogs.com/popfisher/p/5191242.html