如何在android中的字符串中的每个“\n”之后添加行间距?

Hus*_*eky 3 html string android newline spacing

\n正如问题所明确指出的那样,我想在 Android 资源文件中的字符串中的每个定义之后添加一个小行距。

假设我们在 xml 中定义了一个字符串,如下所示:

<string name="line_test">This is the 1st line. This is still the 1st line.\nThis is the 2nd line.\nThis is the 3rd line. This is still the 3rd line.\nThis is the 4th line.</string>
Run Code Online (Sandbox Code Playgroud)

将其设置为 a 时的正常输出TextView将类似于该图像的左侧。我想要实现的是该图像的右侧。

图1

xml中有一些属性,例如:

android:lineSpacingMultiplier="2"
Run Code Online (Sandbox Code Playgroud)

android:lineSpacingExtra="10dp"
Run Code Online (Sandbox Code Playgroud)

但这些属性只会导致这样的结果:

图2

所以我不想在每个新行之后添加行距,因为行的宽度被消耗,但仅在每个定义的\n.

有什么办法可以通过代码或html来实现我想要的吗?通过说 html,我的意思是类似这样的内容,但在添加后进行了一些修改<br/>

textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/>This is the 2nd line.<br/>This is the 3rd line. This is still the 3rd line.<br/>This is the 4th line."));
Run Code Online (Sandbox Code Playgroud)

这更像是我想TextView在每个段落之后以小行间距创建许多段落,并将其全部放在一个字符串中。我认为这可以通过 HTML 来完成,所以这就是我添加 html 标签的原因。


编辑:

我也不想添加两个换行符,因为我只想要一个小的行距,而将两个换行符放在一起将会有一个大的行距,由于我的应用程序中的一些设计问题,我不希望出现这种情况。

Roh*_*5k2 5

像这样给出两个换行符怎么样

<string name="line_test">This is the 1st line. This is still the 1st line.\n\nThis is the 2nd line.\n\nThis is the 3rd line. This is still the 3rd line.\n\nThis is the 4th line.</string>
Run Code Online (Sandbox Code Playgroud)

如果HTML使用两个<br/>标签

textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/><br/>This is the 2nd line.<br/><br/>This is the 3rd line. This is still the 3rd line.<br/><br/>This is the 4th line."));
Run Code Online (Sandbox Code Playgroud)