每行的单独行间距

时间:2012-06-20 00:01:01

标签: android textview

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

示例:

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

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

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

spannable是否提供解决方案?

6 个答案:

答案 0 :(得分:17)

是的,您可以使用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);
    }

}

答案 1 :(得分: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>

答案 2 :(得分:0)

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

LineHeightSpan.Standard

答案 3 :(得分: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截图

enter image description here

(行距原本是为汉字设计的,所以英文的区别可能不太明显。)

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

答案 4 :(得分:-3)

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

答案 5 :(得分:-3)

试试这个:

android:lineSpacingMultiplier = "3"

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

相关问题