How to preserve line breaks in xml <string> resources in Android?

故事扮演 提交于 2019-11-27 06:50:20

问题


I'm trying to use multi-line constants (defined in .xml file under /res/values/ folder), but it seems it's impossible to preserve line breaks there - they all being converted in spaces. I've tried to play with "formatted" attribute of strings (setting it both to "true" and "false", also I've tried wrapping strings in CDATA tags, like this:

<string name="str1">
A
B
C
</string>

<string name="str2" formatted="true">
A
B
C
</string>

<string name="str3" formatted="false">
A
B
C
</string>

<string name="str4"><![CDATA[
A
B
C
]]></string>

<string name="str5" formatted="true"><![CDATA[
A
B
C
]]></string>

<string name="str6" formatted="false"><![CDATA[
A
B
C
]]></string>

All these string declaration variants produce identical results - five-character string "A B C" (line breaks replaced by single space). Is there any way to avoid this?

P.S. I understand that I can use "\n" to insert line breaks, but anyway resulting string will contain spaces in place of actual line-breaks; i.e., following declaration:

<string name="str1">
A\n
B\n
C\n
</string>

results in string "A\n B\n C\n" (every manually inserted line break followed by annoying space). Is there any workaround?..


回答1:


In case people are having problems with this, inserting \n at the end of XML string causes indentation. For example:

<string name="test">
    A\n
    B\n
    C
</string>

Will appear as

A
  B
  C

Instead, you need to insert \n at BEGINNING of XML strings

<string name="test">
    A
    \nB
    \nC
</string>

This will cause the new lines to show up properly.




回答2:


Use another backslash at the end of each line:

<string name="str1">
A\n\
B\n\
C\n\
</string>

This way, you insert line breaks using the "\n" and you escape the line end in the resource file so it is not converted to space.

Still ugly, but it works.




回答3:


To add a newline from XML, you need to add "\n".

This is how you do it.

<string name="testing">A\nB\nC</string>

<TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="4"
        android:text="@string/testing"/>

Hope this helps




回答4:


Use html breaks like this:

<string name="your_string">
<![CDATA[
<p>
    Here is your text.<br/>
    This text is on a new line.<br/>
    So is this text. \n
    This is on the previous line with a space added.
</p>
]]>
</string>



回答5:


My two cents.
I solved using string arrays, loading the resource and then concatenating the elements of the array.
I also find it nice to have each long string in a separate file.

String[] storages = getResources().getStringArray(R.array.storages);
String oneline = new String();
    for (int i = 0; i < storages.length; i++) {
        oneline = oneline.concat(storages[i]);
        if (i < storages.length - 1)
            oneline.concat(System.getProperty("line.separator"));
    }


来源:https://stackoverflow.com/questions/18938403/how-to-preserve-line-breaks-in-xml-string-resources-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!