如何美元的XML和LT p $ pserve换行符;串>在Android的资源呢?美元、换行符、资源、XML

2023-09-04 09:36:33 作者:蔻丹。

我想使用多线常数(在.xml文件中定义的下/ RES /价值/文件夹),但现在看来,这是不可能的preserve换行符在那里 - 他们都被转换的空间。我试着用格式化玩串的属性(这两个设置为真和假,我也试过在CDATA标签包裹的字符串,像这样的:

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>

所有这些字符串声明变种产生相同的结果 - 五字符串ABC(行由单个空格替换符)。有没有什么办法避免这种情况?

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。我明白,我可以用\ n来插入换行符,但无论如何,结果字符串将包含空格代替实际的换行符的;即,声明如下:

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>

结果字符串A \ñ乙\ N c个\ N(每一个手动插入换行符后跟恼人的空间)。请问有什么解决办法?..

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

推荐答案

在情况下的人有问题,这一点,插入在XML字符串的结束导致的压痕。例如:

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>

将显示为

A
  B
  C

相反,你需要插入在XML字符串的开头

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.