2012-12-28 25 views
0

我需要在字符串中添加多个换行符,但不起作用,它只会添加第一个换行符。但在Java中,它的工作原理。Android中字符串中的两个(或多个)连续换行符

例如,对于这个字符串:

String s = "one\n\ntwo"; 

打印在机器人:

one 
two 

在java中打印:

one 

two 

我用 “\ n” 个测试,“系统.getProperty(“line.separator”)和String.format(“%n”)得到相同结果。

有谁知道解决方案?

更新:我动态生成的字符串来对服务器进行身份验证,我有没有必要将它添加到一个TextView,我不能使用的strings.xml

解决:logcat的不打印仅\ n行,但字符串包含它。谢谢。

+0

你在logcat中或TextView的打印? –

+0

我在logcat中打印 – alfonsomiranda

+0

尝试在textview中设置文本并查看您是否获得了结果。 –

回答

0

我觉得你应该把你的字符串中string.xml文件中像这样

<string name="test">one\n\n\ntwo</string> 

明智的做法表明你。

感谢

+0

这对我无效,我动态生成字符串来对服务器进行身份验证。 – alfonsomiranda

0

试试这个:

<TextView 
    android:id="@+id/textId1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:singleLine="false" 
    android:textColor="#ff4500" /> 

从你Activty:

String s = "one\n\ntwo"; 
TextView tv = (TextView) findViewById(R.id.textId1);   
tv.setText(s); 

感谢。

+0

这对我无效,我动态生成字符串以对服务器进行身份验证。 – alfonsomiranda

+1

@alfonsomiranda在您发布问题时要特别注意您的要求。 –

0

尝试添加<br><br/>而不是\ n。

1

使用此

String s = "one \n \n two"; 

&之前换行符(\ n)后,留有一定的空间,然后它会工作

+0

使用字符串对服务器进行身份验证,我无法设置空白。 – alfonsomiranda

相关问题