android上多样式文本的使用

 from stackoverflow.com:

  
  
  
  
  1. SpannableStringBuilder builder = new SpannableStringBuilder(); 
  2.  
  3. String red = "this is red"
  4. SpannableString redSpannable= new SpannableString(red); 
  5. redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0); 
  6. builder.append(redSpannable); 
  7.  
  8. String white = "this is white"
  9. SpannableString whiteSpannable= new SpannableString(white); 
  10. whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0); 
  11. builder.append(whiteSpannable); 
  12.  
  13. String blue = "this is blue"
  14. SpannableString blueSpannable = new SpannableString(blue); 
  15. blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0); 
  16. builder.append(blueSpannable); 
  17.  
  18. mTextView.setText(builder, BufferType.SPANNABLE); 

or

 

  
  
  
  
  1. yourTextView.setText(Html.fromHtml("<FONT COLOR=\"#80776b\" >First color</Font>"+"<FONT COLOR=\"#80776b\" >Second color</Font>")); 

or 

  
  
  
  
  1. String text = "This text is white. <font color=\"blue\">This text is blue.</font>"
  2. textView.setText(Html.fromHtml(text), BufferType.SPANNABLE); 

还有人封装了一下API:

I created a class like this:

  
  
  
  
  1. import android.text.SpannableStringBuilder; 
  2. import android.text.style.CharacterStyle; 
  3.  
  4. public class StyleableSpannableStringBuilder extends SpannableStringBuilder { 
  5.     public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle c, CharSequence text) { 
  6.         super.append(text); 
  7.         int startPos = length() - text.length(); 
  8.         setSpan(c, startPos, length(), 0); 
  9.         return this
  10.     } 
  11.  
  12.     public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle [] c, CharSequence text) { 
  13.         super.append(text); 
  14.         int startPos = length() - text.length(); 
  15.         for (CharacterStyle c1 : c) 
  16.             setSpan(c1, startPos, length(), 0);          
  17.         return this
  18.     }        

This allows me to do things like this:

 

  
  
  
  
  1. private void buildTickerItem(DelayedQuoteServiceObject o) 
  2. {    
  3.     Double lastPrice = Double.parseDouble(o.getValue("LastPrice")); 
  4.     Double dayChange = Double.parseDouble(o.getValue("DayChange")); 
  5.     Double percentChange = Double.parseDouble(o.getValue("PercentDayChange")) / 100
  6.  
  7.     if (o.isIndex() == true
  8.     { 
  9.  
  10.         tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD),o.getDisplayName());             
  11.         tickerTapeData.append(" "+ indexFormat.format(lastPrice) + " ("); 
  12.  
  13.         if (dayChange >= 0
  14.             tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), indexFormat.format(dayChange));         
  15.         else 
  16.             tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), indexFormat.format(dayChange)); 
  17.     } 
  18.     else 
  19.     { 
  20.         tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD), o.ticker); 
  21.  
  22.         tickerTapeData.append("@"+ dollarFormat.format(lastPrice) + " (");               
  23.  
  24.         if (dayChange >= 0
  25.             tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), dollarFormat.format(dayChange));        
  26.         else 
  27.             tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), dollarFormat.format(dayChange)); 
  28.  
  29.  
  30.     } 
  31.  
  32.     tickerTapeData.append("/"); 
  33.  
  34.  
  35.     if (dayChange >= 0
  36.         tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), percentFormat.format(percentChange));       
  37.     else 
  38.         tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), percentFormat.format(percentChange)); 
  39.  
  40.     tickerTapeData.append(")  ");        

 

你可能感兴趣的:(android,textview,样式,文本)