Paragraph spacings using SpannableStringBuilder in TextView

后端 未结 1 959
离开以前
离开以前 2021-01-02 19:07

As the question indicates, I am working on a TextView which will show formatted text using SpannableStringBuilder. It has multiple paragraphs and I

1条回答
  •  有刺的猬
    2021-01-02 19:48

    Implement the LineHeightSpan and override chooseHeight method as follows

    @Override
    public void chooseHeight(CharSequence text, int start, int end,
            int spanstartv, int v, FontMetricsInt fm) {
        Spanned spanned = (Spanned) text;
        int st = spanned.getSpanStart(this);
        int en = spanned.getSpanEnd(this);
        if (start == st) {
            fm.ascent -= TOP_SPACING;
            fm.top -= TOP_SPACING;
        }
        if (end == en) {
            fm.descent += BOTTOM_SPACING;
            fm.bottom += BOTTOM_SPACING;
        }
    }
    

    Don't forget to add \n at the end of your each paragraph text.

    0 讨论(0)
提交回复
热议问题