Spannable.SPAN_PARAGRAPHを使ったsample codeは次のとおりです。
public class TextViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SpannableString ss;
SpannableStringBuilder ssb;
BackgroundColorSpan spanRed, spanGreen, spanBlue;
TextView tv;
ss = new SpannableString("aaa\nbbb\nccc\nddd\n");
ssb = new SpannableStringBuilder();
spanRed = new BackgroundColorSpan(0xff880000);
spanGreen = new BackgroundColorSpan(0xff008800);
spanBlue = new BackgroundColorSpan(0xff000088);
ss.setSpan(spanRed, 4, 8, Spannable.SPAN_PARAGRAPH);
ss.setSpan(spanGreen, 8, 12, Spannable.SPAN_PARAGRAPH);
ss.setSpan(spanBlue, 12, 16, Spannable.SPAN_PARAGRAPH);
ssb.append(ss);
spanRed = new BackgroundColorSpan(0xff880000);
ssb.append("eee\n");
ssb.setSpan(spanRed, 16, 20, Spannable.SPAN_PARAGRAPH);
ssb.append("fff\n");
tv = (TextView)findViewById(R.id.TextView);
tv.setText(ssb);
}
}
このプログラムを実行すると次のように画面に表示されます。
aaabbb
ccc
ddd
eee
fff
0 件のコメント:
コメントを投稿