10

是否可以为 a 的每个文本行定义单独的行距TextView

例子:

TextView tv = new TextView(context);
tv.setText("line1\nline2\nline3");

该方法setLineSpacing(float add, float mult)定义TextView. 我想在 line1 和 line2 之间定义另一个行距,在 line2 和 line3 之间定义一个不同的行距。

任何想法如何做到这一点?

spannable 是否提供解决方案?

4

6 回答 6

19

是的,您可以通过使用LineHeightSpan界面来做到这一点。这是有关如何执行此操作的快速而肮脏的示例代码:

public class MyActivity extends Activity {

    private static class MySpan implements LineHeightSpan {
        private final int height;

        MySpan(int height) {
            this.height = height;
        }

        @Override
        public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
                FontMetricsInt fm) {
            fm.bottom += height;
            fm.descent += height;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TextView tv = new TextView(this);
        setContentView(tv);

        tv.setText("Lines:\n", BufferType.EDITABLE);
        appendLine(tv.getEditableText(), "Line 1 = 40\n", 40);
        appendLine(tv.getEditableText(), "Line 2 = 30\n", 30);
        appendLine(tv.getEditableText(), "Line 3 = 20\n", 20);
        appendLine(tv.getEditableText(), "Line 4 = 10\n", 10);
    }

    private void appendLine(Editable text, String string, int height) {
        final int start = text.length();
        text.append(string);
        final int end = text.length();
        text.setSpan(new MySpan(height), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

}
于 2012-06-20T13:03:26.653 回答
1

您可以使用 Html 对 textview 执行这些特定样式。试试这个例子,

tv.setText(Html.fromHtml("<h2>Text1</h2><br><p>Text2</p>"));

这里允许的不同类型的标签是,

<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>
于 2012-06-20T02:59:34.287 回答
1

添加了Android 10 (API 29)LineHeightSpan.Standard以设置绝对行高。如果您需要向后兼容,请使用我基于的以下实现LineHeightSpan.Standard

private static class AbsoluteHeightSpan implements LineHeightSpan {
    private final int _height;

    AbsoluteHeightSpan(int height) {
        _height = height;
    }

    @Override
    public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, FontMetricsInt fm) {
        final int originHeight = fm.descent - fm.ascent;
        // If original height is not positive, do nothing
        if (originHeight <= 0)
            return;
        final float ratio = _height * 1.0f / originHeight;
        fm.descent = Math.round(fm.descent * ratio);
        fm.ascent = fm.descent - _height;
    }
}
于 2019-11-26T10:04:42.080 回答
0

最近我面临着类似的问题。我使用来自的字符串资源string.xml并将其显示在对话框中。我通过在内部使用<font>标签<string>并在其中指定字体大小来解决我的问题\n。第一个\n只是一个换行符,第二个是一个新的空行,高度由<font size="YOUR_SIZE_FOR_THE_LINE"></font>.

示例代码:

    <string name="specification_msg"><b>1 Quick Action</b>
        \nQuick-Action is a small window which is supposed to be fast so it can offer you a convenient way of recording income and expenditure.
        <font size="4">\n\n</font><b>︎︎1.1 Brief Mode</b>
        \nDate and time will be hidden in window and set to current time. If you scarcely adjust them, turning on this feature will give you a cleaner view.
        <!-- Other lines -->
    </string>

截图:AlertDialog里面TextView的截图

在此处输入图像描述

(行间距最初是为汉字设计的,因此在英文中可能看起来不太明显。)

我知道这是一种肮脏的方法,但它有时会有所帮助......

于 2021-04-22T04:46:18.653 回答
-3

这是不可能的。这种技术在 c ,c++ 中使用。你在 setText() 中使用什么,它显示你写的整个文本。

于 2012-06-20T03:03:11.800 回答
-3

尝试这个:

android:lineSpacingMultiplier = "3"

行间距也可以是十进制格式。

于 2015-03-31T09:15:10.683 回答