在开发过程中,本人发现有时候TextView需要特殊显示的时候,需要特殊处理,现将我的代码记录如下
1.为TextView 添加下划线
TextView appVersion=(TextView) root.findViewById(R.id.appversion_value);
appVersion.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
appVersion.setText("Ver 1.2.5");
如上代码这样就为这样一个代码添加了下划线
2.为TextView添加超链接
String blogsite="http://blog.csdn.net/yangqicong11";
SpannableString spblog = new SpannableString( blogsite);
//设置超链接
spblog.setSpan( new URLSpan( blogsite ), 0 , blogsite.length() ,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
PersonalBlog=(TextView) root.findViewById(R.id.PersonalBlog_value);
PersonalBlog.setText(spblog);
PersonalBlog.setMovementMethod(LinkMovementMethod.getInstance());
这样我们的TextView就成了超链接的形式,用户点击后可以直接调用浏览器跳转到对应页面
3.为TextView 添加背景高亮显示
String tmp= "背景高亮";
SpannableString sp = new SpannableString( "背景高亮" );
//设置高亮样式一
sp.setSpan( new BackgroundColorSpan(Color.BLUE), 0 , tmp.length() ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView appEditor=(TextView) root.findViewById(R.id.appEditor_value);
appEditor.setText(sp);
这样我们的TextView 背景就变成高亮了
4.为TextView 添加文字高亮显示
String tmp= "文字高亮";
SpannableString sp = new SpannableString( "文字高亮" );
//设置高亮样式二
sp.setSpan( new ForegroundColorSpan(Color.YELLOW), 0 , tmp.length() ,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
TextView appEditor=(TextView) root.findViewById(R.id.appEditor_value);
appEditor.setText(sp);
与上一个所不同的是 这里仅仅只是TextView中的文字产生了高亮的效果
5.为TextView添加加粗斜体显示
String tmp= "设置斜体";
SpannableString sp = new SpannableString( "设置斜体" );
//设置斜体
sp.setSpan( new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0 , tmp.length() , Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
TextView appEditor=(TextView) root.findViewById(R.id.appEditor_value);
appEditor.setText(sp);