つまり、1つの文字列で、"This text is blue"というテキストを青色に変更することは可能なのでしょうか?何か方法があるはずなのですが・・・。
<TextView
android:gravity="left"
android:padding="3dip"
android:text="This text is white. This text is blue."
android:textColor="#ffffff"
android:textSize="22dp"/>
はい、可能です。このためには、SpanableString
と ForegroundColorSpan
を使用する必要があります。
これは次のようなものです。
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);
。
簡単な方法としては、HTMLを使用して、プログラム的に TextView
にテキストを設定します。
String text = "This text is white. <font color=\"blue\">This text is blue.</font>";
textView.setText(Html.fromHtml(text), BufferType.SPANNABLE);
こんなクラスを作りました。
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;
}
}
これによって、こんなことができるようになりました。
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(") ");
}
ティッカーテープを作成する。 かなりきれいに動作し、コードもきれいに保たれます。