【问题标题】:What is Leading Margin in Android?什么是 Android 的领先优势?
【发布时间】:2017-06-21 16:35:56
【问题描述】:

中的前导边距是什么意思

LeadingMarginSpansays 的文档

影响页边距的段落样式。可以有多个 前导边距跨越单个段落;它们将呈现在 顺序,每个都将其边距添加到它之前的边距。领先的 对于从右到左的段落中的行,边距位于右侧。

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

它就像段落第一行的制表符缩进吗?还是整个段落缩进的地方?我假设它是 /lidɪŋ/ 而不是 /lɛdɪŋ/ as in the space between lines

我想知道的原因是我正在尝试使用 StaticLayout 制作自己的 TextView。我指的是 Layout 和 StaticLayout source code 的想法。我正在尝试删除所有不必要的部分,但我不知道这是什么。

这里有几个 SO 问题也询问了领先优势,但提问者似乎知道它的含义。

图片会很有帮助,但不是绝对必要的。

【问题讨论】:

    标签: android margins staticlayout leadingmarginspan2


    【解决方案1】:

    前导边距是指段落的缩进量,包括首行和后续行。

    下面的例子应该让一切都清楚。以下示例中的 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 告诉每个段落的其余行缩进多少像素。

    左边的示例将第一行缩进 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 告诉每个段落的每一行缩进多少像素。

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

    LeadingMarginSpan span = new LeadingMarginSpan.Standard(200);
    

    【讨论】:

    • 为什么将前导边距跨度设置为 EXCLUSIVE_EXCLUSIVE 会使键入的字符出现在editText中的光标选择之后。
    • @SyedHissaan 我不确定。它应该是另一个设置吗? stackoverflow.com/a/41949135
    • 谢谢,经过大量研究,我终于自己弄清楚了。实际上,当应用前导边距时,android 将文本换行到下一行的方式存在问题。解决方案是删除跨度并在行数增加后重新应用它。
    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 2011-03-10
    • 2011-03-25
    相关资源
    最近更新 更多