安卓中的Leading Margin是什么?

22
什么是leading margin的含义,例如:

LeadingMarginSpan的文档

这是一种影响前导空白边距的段落样式。一个段落中可以有多个前导空白边距,它们将按顺序呈现,每个边距都会添加到之前的边距上。对于从右到左的段落,前导空白边距在右侧。

但它并没有真正说明前导空白边距是什么。

它是像段落第一行的制表符缩进吗?还是整个段落都缩进了?我假设它是/lidɪŋ/而不是/lɛdɪŋ/,如行间距之间的空格

我想知道的原因是,我正在尝试使用StaticLayout制作自己的TextView。我参考了Layout和StaticLayout 源代码 的想法。我试图削减所有不必要的部分,但我不知道这是什么意思。
以下是一些关于前导边距的SO问题,但提问者似乎知道它的含义。 一个图片会很有帮助,但不是必需的。
1个回答

35

前导缩进是指一个段落的首行和后续行的缩进量。

下面的示例将让一切更加清晰。以下示例中的TextView包含两个文本段落(即它们包括\n字符)。

这是使用的样板代码:

LeadingMarginSpan span = ... // substitute this line with the examples below

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

有两个主要的构造函数。

第一个构造函数:LeadingMarginSpan.Standard(int first, int rest)

  • first指定了每个段落的第一行缩进多少像素。
  • rest指定了每个段落其余行的缩进像素数。

enter image description here

左侧示例将第一行缩进20像素,其余行缩进100像素。(TextView没有添加填充。)

LeadingMarginSpan span = new LeadingMarginSpan.Standard(20, 100); // left example

右侧的示例显示第一行缩进了100,而其余行则没有缩进。

LeadingMarginSpan span = new LeadingMarginSpan.Standard(100, 0); // right exmaple

第二个构造函数:LeadingMarginSpan.Standard(int every)

  • every 指定每个段落每行要缩进的像素数。

enter image description here

此示例将每行缩进200像素。

LeadingMarginSpan span = new LeadingMarginSpan.Standard(200);

为什么将leading margin span设置为EXCLUSIVE_EXCLUSIVE会使得在editText中输入的字符出现在光标选择后面? - Hissaan Ali
@SyedHissaan 我不确定。它应该是另一个设置吗?https://dev59.com/8Wkw5IYBdhLWcg3wdKQv#41949135 - Suragch
1
谢谢,经过大量的研究,我终于自己解决了问题。实际上,这是一个与Android在应用前导边距时换行文本方式有关的问题。解决方法是在行数增加后删除span并重新应用它。 - Hissaan Ali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接