본문 바로가기

Android

Font Color 주기(Reusable Spans)


TextView t = (TextView) findViewById(R.id.tv);

final SpannableStringBuilder sp = new SpannableStringBuilder("foobar
\n");

sp.setSpan(new ForegroundColorSpan(Color.RED), 0, sp.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

final SpannableStringBuilder sp2 = new SpannableStringBuilder("foo ");
sp2.append(sp);

t.append(sp2); //ok, foobar is red

final SpannableStringBuilder sp3 = new SpannableStringBuilder("bar ");
sp3.append(sp);

t.append(sp3); //foobar is no longer red