from stackoverflow.com:
- SpannableStringBuilder builder = new SpannableStringBuilder();
- String red = "this is red";
- SpannableString redSpannable= new SpannableString(red);
- redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
- builder.append(redSpannable);
- String white = "this is white";
- SpannableString whiteSpannable= new SpannableString(white);
- whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0);
- builder.append(whiteSpannable);
- String blue = "this is blue";
- SpannableString blueSpannable = new SpannableString(blue);
- blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
- builder.append(blueSpannable);
- mTextView.setText(builder, BufferType.SPANNABLE);
or
- yourTextView.setText(Html.fromHtml("<FONT COLOR=\"#80776b\" >First color</Font>"+"<FONT COLOR=\"#80776b\" >Second color</Font>"));
or
- String text = "This text is white. <font color=\"blue\">This text is blue.</font>";
- textView.setText(Html.fromHtml(text), BufferType.SPANNABLE);
还有人封装了一下API:
I created a class like this:
- import android.text.SpannableStringBuilder;
- import android.text.style.CharacterStyle;
- public class StyleableSpannableStringBuilder extends SpannableStringBuilder {
- public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle c, CharSequence text) {
- super.append(text);
- int startPos = length() - text.length();
- setSpan(c, startPos, length(), 0);
- return this;
- }
- public StyleableSpannableStringBuilder appendWithStyle(CharacterStyle [] c, CharSequence text) {
- super.append(text);
- int startPos = length() - text.length();
- for (CharacterStyle c1 : c)
- setSpan(c1, startPos, length(), 0);
- return this;
- }
- }
This allows me to do things like this:
- private void buildTickerItem(DelayedQuoteServiceObject o)
- {
- Double lastPrice = Double.parseDouble(o.getValue("LastPrice"));
- Double dayChange = Double.parseDouble(o.getValue("DayChange"));
- Double percentChange = Double.parseDouble(o.getValue("PercentDayChange")) / 100;
- if (o.isIndex() == true)
- {
- tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD),o.getDisplayName());
- tickerTapeData.append(" "+ indexFormat.format(lastPrice) + " (");
- if (dayChange >= 0)
- tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), indexFormat.format(dayChange));
- else
- tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), indexFormat.format(dayChange));
- }
- else
- {
- tickerTapeData.appendWithStyle(new StyleSpan(Typeface.BOLD), o.ticker);
- tickerTapeData.append("@"+ dollarFormat.format(lastPrice) + " (");
- if (dayChange >= 0)
- tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), dollarFormat.format(dayChange));
- else
- tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), dollarFormat.format(dayChange));
- }
- tickerTapeData.append("/");
- if (dayChange >= 0)
- tickerTapeData.appendWithStyle(new ForegroundColorSpan(Colours.getTickerPositive()), percentFormat.format(percentChange));
- else
- tickerTapeData.appendWithStyle(new ForegroundColorSpan(Color.RED), percentFormat.format(percentChange));
- tickerTapeData.append(") ");
- }