2012-12-28 23 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

相關問題