android中TextView分段显示不同颜色,字体

 public class TextViewLinkActivity extends Activity {       
TextView myTextView;       
@Override     
 public void onCreate(Bundle savedInstanceState) {           
super.onCreate(savedInstanceState);           
setContentView(R.layout.main);           
myTextView = (TextView) this.findViewById(R.id.myTextView);                      
//创建一个 SpannableString对象          
SpannableString sp = new SpannableString("这句话中有百度超链接,有高亮显示,这样,或者这样,还有斜体.");           
//设置超链接         
 sp.setSpan(new URLSpan("http://www.baidu.com"), 5, 7,                   
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);          
 //设置高亮样式一         
 sp.setSpan(new BackgroundColorSpan(Color.RED), 17 ,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);           
//设置高亮样式二          sp.setSpan(new ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);             
//设置斜体          
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27, 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);          
 //SpannableString对象设置给TextView          
myTextView.setText(sp);          
 //设置TextView可点击        
  myTextView.setMovementMethod(LinkMovementMethod.getInstance());      
 }   
第二种方法 :  tv.setText(Html.fromHtml("是<font color=blue>StringText</font>text2"));

 此文转自http://hi.baidu.com/miky%D1%EE_1987/blog/item/b55dfe1462a1ec10314e152c.html

猜你喜欢

转载自zxuenet.iteye.com/blog/1633322