关于多段的LeadingMarginSpan

时间:2014-11-03 15:57:28

标签: android textview spannablestring spannable

我在一个TextView中有多个段落,我试图实现一种“表格式”效果。第一行有一个零缩进和一个制表符缩进(我为此使用TabStopSpan),其余的使用LeadingMarginSpan.Standard(0, INDENTATION)作为效果。

看起来像这样:

正如您所看到的,缩进应用于两个段落,但是线条正确地断开,仅在第一个段落中,并且它们在第二个段落中脱离屏幕。以下是我用来实现此代码的代码:

public class MyActivity extends Activity implements {

DemoTextView demoTextView;
int columnIndentation = 150;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    demoTextView = (DemoTextView) findViewById(R.id.demo_textview);

    SpannableStringBuilder demoSpannableString = getDemoSpannable();
    demoSpannableString.append("\n\n");
    demoSpannableString.append(getDemoSpannable());
    demoTextView.setText(demoSpannableString, TextView.BufferType.SPANNABLE);
}

private SpannableStringBuilder getDemoSpannable () {
    SpannableStringBuilder demoSpannableString = new SpannableStringBuilder(getDemoText());
    demoSpannableString.setSpan(new TabStopSpan() {
        @Override
        public int getTabStop() {
            return columnIndentation;
        }
    }, 0, demoSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    demoSpannableString.setSpan(new LeadingMarginSpan.Standard(0, columnIndentation), 0, demoSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    demoSpannableString.setSpan(new MyTypefaceSpan(this, TIMES_NEW_ROMAN), 0, demoSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return demoSpannableString;
}

public String getDemoText () {
    return "Lorem \tipsum dolor sit amet, consectetur adipiscing elit. Donec lobortis condimentum tincidunt. Suspendisse leo risus, auctor sit amet erat et, ultricies fringilla augue. Integer feugiat elementum turpis, non finibus mi sodales vitae. Proin scelerisque hendrerit orci, in viverra nulla. Curabitur tempor orci non metus semper, ut vehicula massa euismod. Phasellus ultrices ipsum sit amet lobortis imperdiet. Aenean libero nulla, vestibulum id efficitur volutpat, feugiat sagittis nunc.";
}
}

任何想法如何避免文本在后续段落中脱离屏幕?

1 个答案:

答案 0 :(得分:2)

这似乎是一个影响3.0以上所有Android版本的bug,任何 LeadingMarginSpan 其中' first'并且'休息'不会同样会受到这个问题的影响。如果您使用

LeadingMarginSpan.Standard(columnIndentation)

而不是

LeadingMarginSpan.Standard(0, columnIndentation)

第二段不会被修剪,但 TabStopSpan 将无效。

检查相关问题,https://code.google.com/p/android/issues/detail?id=38003