如何在 android 中的字符串中的每个“\n”后添加行间距?
How to add line spacing after every "\n" in a string in android?
问题说得很清楚了,我想在Android.
中的资源文件的字符串中定义的每个\n
后添加一个小行距
假设我们在 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
时的正常输出将像此图像的左侧。我要实现的是这个图像的右侧。
xml 中有一些属性,例如:
android:lineSpacingMultiplier="2"
和
android:lineSpacingExtra="10dp"
但是这些属性只会导致这样的结果:
所以我不想在每一个新行之后添加行间距,因为行的宽度被消耗了,而是只在每个定义的 \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."));
你可以试试这个:
public String addNewLine(String string) {
int count = string.split("\n", -1).length - 1;
StringBuilder sb = new StringBuilder(count);
String[] splitString = string.split("\n");
for (int i = 0; i < splitString.length; i++) {
sb.append(splitString[i]);
if (i != splitString.length - 1) sb.append("\n\n");
}
return sb.toString();
}
问题说得很清楚了,我想在Android.
中的资源文件的字符串中定义的每个\n
后添加一个小行距
假设我们在 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
时的正常输出将像此图像的左侧。我要实现的是这个图像的右侧。
xml 中有一些属性,例如:
android:lineSpacingMultiplier="2"
和
android:lineSpacingExtra="10dp"
但是这些属性只会导致这样的结果:
所以我不想在每一个新行之后添加行间距,因为行的宽度被消耗了,而是只在每个定义的 \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."));
你可以试试这个:
public String addNewLine(String string) {
int count = string.split("\n", -1).length - 1;
StringBuilder sb = new StringBuilder(count);
String[] splitString = string.split("\n");
for (int i = 0; i < splitString.length; i++) {
sb.append(splitString[i]);
if (i != splitString.length - 1) sb.append("\n\n");
}
return sb.toString();
}