在TextView中使用SpannableStringBuilder的段落间距 [英] Paragraph spacings using SpannableStringBuilder in TextView
问题描述
正如问题所示,我正在研究TextView
,它将使用SpannableStringBuilder
显示格式化的文本.它有多个段落,我想知道使用某些内置跨度在段落之间设置间距的最简单(或至少最简单)的方法.这可能吗?还是需要为此构建一个自定义的span类?
As the question indicates, I am working on a TextView
which will show formatted text using SpannableStringBuilder
. It has multiple paragraphs and I would like to know what would be the easiest (or at least the least complicated) way to set spacing between paragraphs using some inbuilt span. Is this possible? Or will I be required to build a custom span class for this?
推荐答案
实施LineHeightSpan
并覆盖chooseHeight
方法,如下所示
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;
}
}
别忘了在每个段落文本的末尾添加\n
.
Don't forget to add \n
at the end of your each paragraph text.
这篇关于在TextView中使用SpannableStringBuilder的段落间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!