2015-05-09 211 views
0

我正在開發一個具有長文本的android應用程序。 我把它們放在一個string-array.One項目看起來像這樣如何在android中保持字符串數組項的文本換行符?

<string-array name="data"> 
 
    <item> 
 
    Bangladesh has been aptly described as a new state in an ancient land. 
 
    
 
    Much has been written about the past glory of Bangladesh, notably in old records like the evidence of Pliny and Periplus of the Erythrean Sea (first century AD). 
 
    
 
    They include the Chinese travellers Fa-hien (fourth century AD), Hue-an-tsung (seventh century), Ma-hoen and Fei-shin (fifteenth century), Ibne Batuta (fourteenth century) from Africa, Nicola Kanti (fifteenth century) and Ceasar the Frederik (sixteenth century) from Venice, Verthema, an Italian in the sixteenth century, Barbosa and Sebastin Manric (sixteenth century) from Portugal, Travernier and Bernier from France (seventeenth century) and Queen Elizabeth-the First's ambassador Ralf Fish. 
 
    
 
    Etymologically, the word Bangladesh is derived from the cognate Vanga which was first mentioned in Aitarey Aranyaka, a Hindu scripture composed between 500 BC and 500 AD. 
 
    
 
    
 
    </item> 
 
</string-array>

當我在我的Activity類的文本,放在一個TextView我看起來是這樣的:

孟加拉國被恰當地描述爲古代土地上的新國家。 關於孟加拉國過去的輝煌,尤其是在古老的記錄(如普林尼和紅海的佩裏普盧斯證據(公元1世紀))中已有大量的文章。其中包括中國遊客法師(公元4世紀),順化(十七世紀),馬霍恩與費新(十五世紀),來自非洲的伊本巴圖塔(十四世紀),尼科拉坎蒂(十五世紀)和來自威尼斯的弗雷德裏克(十六世紀)的塞薩爾,意大利人維泰赫瑪十六世紀,來自葡萄牙的Barbosa和Sebastin Manric(十六世紀),來自法國(十七世紀)的Travernier和Bernier以及伊麗莎白女王 - 第一位的大使拉爾夫魚。在詞源學上,孟加拉國這個詞來源於同名Vanga,這個詞在Aitarey Aranyaka中首次提到,這是一部印度教經文,由公元前500年至公元500年之間組成。

因此,所有新線gone.I可以通過將「\ n」行
之後添加新行,但我有一個巨大的一套這樣的數據。所以手動添加它們將會很困難。
當我通過調用getResources.getString-array(data)來獲取它們時,是否有任何方法可以保持文本的相同形式?
我沒有使用這個項目的任何數據庫。
在此先感謝。

回答

0

爲此,您可以將HTML文本中的strings.xml文件,這樣一來:

<string name="text"> 
    <![CDATA[ 
     </br>Bangladesh has been aptly described as a new state in an ancient land.</br></br> 

     Much has been written about the past glory of Bangladesh, notably in old records like the evidence of Pliny and Periplus of the Erythrean Sea (first century AD).</br></br> 

     They include the Chinese travellers Fa-hien (fourth century AD), Hue-an-tsung (seventh century), Ma-hoen and Fei-shin (fifteenth century), Ibne Batuta (fourteenth century) from Africa, Nicola Kanti (fifteenth century) and Ceasar the Frederik (sixteenth century) from Venice, Verthema, an Italian in the sixteenth century, Barbosa and Sebastin Manric (sixteenth century) from Portugal, Travernier and Bernier from France (seventeenth century) and Queen Elizabeth-the First\'ambassador Ralf Fish.</br></br> 

     Etymologically, the word Bangladesh is derived from the cognate Vanga which was first mentioned in Aitarey Aranyaka, a Hindu scripture composed between 500 BC and 500 AD. 

    ]]> 
</string> 

而在你的活動設置這樣的文字:

TextView textView = (TextView)findViewById(R.id.textview); 
textView.setText(Html.fromHtml(getString(R.string.html))); 
+0

增加休息和段落標記爲根據您所需的格式。 –

+1

其實我可以通過\ n和\ t手動添加 我的問題:有沒有什麼方法可以通過編程方式保持格式化/以其他方式手動添加任何東西(
作爲答案),因爲我的數據集很大? – Alvi

相關問題