1 简介
之前已经讲过TextView的基础知识,现在在这进一步进行讲解,这篇文字主要讲解如何给TextView设置段落级别的Span。如果一个Span想要影响段落层次的文本格式,则需要实现ParagraphStyle。
2 ParagraphStyle
ParagraphStyle是一个接口,通过查看Android源码,我们发现这个接口里面什么方法也没有定义,因此,我们可以认为,这个接口无非是标识实现这个接口的Span为段落级别的Span。
在Android源码中又继续定义了几个接口实现了ParagraphStyle接口。
ParagraphStyle
LeadingMarginSpan:用来处理像word中项目符号一样的接口;
AlignmentSpan:用来处理整个段落对其的接口;
LineBackgroundSpan:用来处理一行的背景的接口;
LineHeightSpan:用来处理一行高度的接口;
TabStopSpan:用来将字符串中的"\t"替换成相应的空行;
3 LeadingMarginSpan
LeadingMarginSpan用来控制整个段落左边或者右边显示某些特定效果,里面有两个接口方法。
/**
* Returns the amount by which to adjust the leading margin. Positive values
* move away from the leading edge of the paragraph, negative values move
* towards it.
*
* @param first true if the request is for the first line of a paragraph,
* false for subsequent lines
* @return the offset for the margin.
*/
public int getLeadingMargin(boolean first);
/**
* Renders the leading margin. This is called before the margin has been
* adjusted by the value returned by {@link #getLeadingMargin(boolean)}.
*
* @param c the canvas
* @param p the paint. The this should be left unchanged on exit.
* @param x the current position of the margin
* @param dir the base direction of the paragraph; if negative, the margin
* is to the right of the text, otherwise it is to the left.
* @param top the top of the line
* @param baseline the baseline of the line
* @param bottom the bottom of the line
* @param text the text
* @param start the start of the line
* @param end the end of the line
* @param first true if this is the first line of its paragraph
* @param layout the layout containing this line
*/
public void drawLeadingMargin(Canvas c, Paint p,
int x, int dir,
int top, int baseline, int bottom,
CharSequence text, int start, int end,
boolean first, Layout layout);
LeadingMarginSpan2还多规定了一个方法。
/**
* Returns the number of lines of the