我尝试使用此代码创建一个带有字符串“abcde”的TextView,其中“b”和“d”跨越:
private void testStringSpanning(TextView tv)
{
// blue
ForegroundColorSpan fcs = new ForegroundColorSpan( Color.BLUE );
SpannableString a = new SpannableString( "a" );
SpannableString b = new SpannableString( "b" );
b.setSpan( fcs, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
SpannableString c = new SpannableString( "c" );
SpannableString d = new SpannableString( "d" );
d.setSpan( fcs, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
SpannableString e = new SpannableString( "e" );
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(a);
builder.append(b);
builder.append(c);
builder.append(d);
builder.append(e);
tv.setText( builder, BufferType.SPANNABLE);
}
结果不是我想要的。a、c 和 e 应为纯白色,bd 应为蓝色。
我究竟做错了什么?