SpannableStringBuilder

设置指定文字颜色:

Java代码 复制代码 收藏代码
  1. String str = "There are 36 apps in your phone."
  2. int index = appNum.indexOf("apps"); 
  3. SpannableStringBuilder ss = new SpannableStringBuilder(str); 
  4. ForegroundColorSpan span=new ForegroundColorSpan(Color.parseColor("#F79719"));   
  5. ss.setSpan(span,16, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
  6. textView.setText(ss)       
String str = "There are 36 apps in your phone.";
int index = appNum.indexOf("apps");
SpannableStringBuilder ss = new SpannableStringBuilder(str);
ForegroundColorSpan span=new ForegroundColorSpan(Color.parseColor("#F79719"));  
ss.setSpan(span,16, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss)      

Java代码 复制代码 收藏代码
  1. ss.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);       
ss.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      

设置指定文字大小:

Java代码 复制代码 收藏代码
  1. String param = "abc"  ; 
  2. String text = param  + "dfdjkfjdkfjdfjdkf"
  3.  
  4. SpannableStringBuilder ss = new SpannableStringBuilder(text); 
  5. ss.setSpan(new StyleSpan(Typeface.BOLD),0, param.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
  6. ss.setSpan(new AbsoluteSizeSpan(30,true), 0, param.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  7. ss.setSpan(new AbsoluteSizeSpan(16,true), param.length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  8. textView.setText(ss); 
String param = "abc"  ;
String text = param  + "dfdjkfjdkfjdfjdkf" ;

SpannableStringBuilder ss = new SpannableStringBuilder(text);
ss.setSpan(new StyleSpan(Typeface.BOLD),0, param.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new AbsoluteSizeSpan(30,true), 0, param.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
ss.setSpan(new AbsoluteSizeSpan(16,true), param.length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(ss);

文字加下划线:

Java代码 复制代码 收藏代码

  1. public void underline(int start,int end){   
  2.         SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString());   
  3.         CharacterStyle span=new UnderlineSpan();   
  4.         spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
  5.         setText(spannable);   
  6.     }   

 

 

 

原文:http://justwyy.iteye.com/blog/1498231

你可能感兴趣的:(Android)