gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:49:48 25 4
gpt4 key购买 nike

问题说得很清楚了,我想在每个 \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>

将其设置为 TextView 时的正常输出将像这张图片的左侧。我想要实现的是这张图片的右侧。

Image 1

xml中有一些属性,比如:

android:lineSpacingMultiplier="2"

android:lineSpacingExtra="10dp"

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

Image 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."));

这更像是我想在 TextView 中创建许多段落每个段落后的行距都在一个字符串中。我认为这可以通过 HTML 完成,所以我添加了 html 标签。


编辑:

我也不想添加两个换行符,因为我只想要一个小的行距,并且将两个换行符放在一起会有很大的行距,由于我的应用程序中的一些设计问题,我不想要.

最佳答案

这样给两个换行怎么样

<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>

如果是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."));

关于android - 如何在android中的字符串中的每个 "\n"之后添加行间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com