前导缩进是指一个段落的首行和后续行的缩进量。
下面的示例将让一切更加清晰。以下示例中的TextView包含两个文本段落(即它们包括\n
字符)。
这是使用的样板代码:
LeadingMarginSpan span = ...
TextView textView = (TextView) findViewById(R.id.textView) ;
SpannableString spannableString = new SpannableString("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
spannableString.setSpan(span, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
有两个主要的构造函数。
第一个构造函数:LeadingMarginSpan.Standard(int first, int rest)
first
指定了每个段落的第一行缩进多少像素。
rest
指定了每个段落其余行的缩进像素数。
![enter image description here](https://istack.dev59.com/ife6f.webp)
左侧示例将第一行缩进20像素,其余行缩进100像素。(TextView
没有添加填充。)
LeadingMarginSpan span = new LeadingMarginSpan.Standard(20, 100)
右侧的示例显示第一行缩进了100,而其余行则没有缩进。
LeadingMarginSpan span = new LeadingMarginSpan.Standard(100, 0)
第二个构造函数:LeadingMarginSpan.Standard(int every)
![enter image description here](https://istack.dev59.com/QFzyd.webp)
此示例将每行缩进200像素。
LeadingMarginSpan span = new LeadingMarginSpan.Standard(200)