from stackoverflow.com:

        
  1. SpannableStringBuilderbuilder=newSpannableStringBuilder();
  2. Stringred="thisisred";
  3. SpannableStringredSpannable=newSpannableString(red);
  4. redSpannable.setSpan(newForegroundColorSpan(Color.RED),0,red.length(),0);
  5. builder.append(redSpannable);
  6. Stringwhite="thisiswhite";
  7. SpannableStringwhiteSpannable=newSpannableString(white);
  8. whiteSpannable.setSpan(newForegroundColorSpan(Color.WHITE),0,white.length(),0);
  9. builder.append(whiteSpannable);
  10. Stringblue="thisisblue";
  11. SpannableStringblueSpannable=newSpannableString(blue);
  12. blueSpannable.setSpan(newForegroundColorSpan(Color.BLUE),0,blue.length(),0);
  13. builder.append(blueSpannable);
  14. mTextView.setText(builder,BufferType.SPANNABLE);

or

        
  1. yourTextView.setText(Html.fromHtml("<FONTCOLOR=\"#80776b\">Firstcolor</Font>"+"<FONTCOLOR=\"#80776b\">Secondcolor</Font>"));

or

        
  1. Stringtext="Thistextiswhite.<fontcolor=\"blue\">Thistextisblue.</font>";
  2. textView.setText(Html.fromHtml(text),BufferType.SPANNABLE);

还有人封装了一下API:

I created a class like this:

        
  1. importandroid.text.SpannableStringBuilder;
  2. importandroid.text.style.CharacterStyle;
  3. publicclassStyleableSpannableStringBuilderextendsSpannableStringBuilder{
  4. publicStyleableSpannableStringBuilderappendWithStyle(CharacterStylec,CharSequencetext){
  5. super.append(text);
  6. intstartPos=length()-text.length();
  7. setSpan(c,startPos,length(),0);
  8. returnthis;
  9. }
  10. publicStyleableSpannableStringBuilderappendWithStyle(CharacterStyle[]c,CharSequencetext){
  11. super.append(text);
  12. intstartPos=length()-text.length();
  13. for(CharacterStylec1:c)
  14. setSpan(c1,startPos,length(),0);
  15. returnthis;
  16. }
  17. }

This allows me to do things like this:

        
  1. privatevoidbuildTickerItem(DelayedQuoteServiceObjecto)
  2. {
  3. DoublelastPrice=Double.parseDouble(o.getValue("LastPrice"));
  4. DoubledayChange=Double.parseDouble(o.getValue("DayChange"));
  5. DoublepercentChange=Double.parseDouble(o.getValue("PercentDayChange"))/100;
  6. if(o.isIndex()==true)
  7. {
  8. tickerTapeData.appendWithStyle(newStyleSpan(Typeface.BOLD),o.getDisplayName());
  9. tickerTapeData.append(""+indexFormat.format(lastPrice)+"(");
  10. if(dayChange>=0)
  11. tickerTapeData.appendWithStyle(newForegroundColorSpan(Colours.getTickerPositive()),indexFormat.format(dayChange));
  12. else
  13. tickerTapeData.appendWithStyle(newForegroundColorSpan(Color.RED),indexFormat.format(dayChange));
  14. }
  15. else
  16. {
  17. tickerTapeData.appendWithStyle(newStyleSpan(Typeface.BOLD),o.ticker);
  18. tickerTapeData.append("@"+dollarFormat.format(lastPrice)+"(");
  19. if(dayChange>=0)
  20. tickerTapeData.appendWithStyle(newForegroundColorSpan(Colours.getTickerPositive()),dollarFormat.format(dayChange));
  21. else
  22. tickerTapeData.appendWithStyle(newForegroundColorSpan(Color.RED),dollarFormat.format(dayChange));
  23. }
  24. tickerTapeData.append("/");
  25. if(dayChange>=0)
  26. tickerTapeData.appendWithStyle(newForegroundColorSpan(Colours.getTickerPositive()),percentFormat.format(percentChange));
  27. else
  28. tickerTapeData.appendWithStyle(newForegroundColorSpan(Color.RED),percentFormat.format(percentChange));
  29. tickerTapeData.append(")");
  30. }

更多相关文章

  1. 虚拟机Dalvik
  2. 使用命令行编译Qt Android(安卓)apps
  3. 最近,又有人在谈论Android的前景了...
  4. 最近,又有人在谈论Android的前景了!深入解析开发趋势及必备技术点
  5. cocos2dx 移植android 报错
  6. 无法安装android sdk
  7. 刘燕丰胸霜是正规产品吗?刘燕酿制丰胸有人成功的吗?
  8. 有人用刘燕酿制丰胸成功吗,产品管用吗?
  9. 有人梦到国自然,我居然梦到看文献!

随机推荐

  1. Linux第二篇----使用rpm方法安装MySQL(含
  2. mysql-5.5配置主从 及 主主关系
  3. 在Windows Mobile和MySQL之间同步DB
  4. 如何使用mysqldump在帐户之间复制视图?
  5. MySQL数据库引擎简介
  6. 如何使用MySql和PHP存储/处理多个表的数
  7. 使用cmake安装mysql5.5.13
  8. Linux测试环境搭建apache+mysql+php
  9. 如何使用本地phmyadmin客户端访问远程服
  10. Failed while installing Dynamic Web Mo