android strings.xml轉義字符, 注意細節解决

XML轉義字符
以下为XML標志符的數字和字符串轉義符
"     (" 或 ")
'     (' 或 ')
&     (& 或 &)
lt(<) (&#60; 或 &lt;)
gt(>) (&#62; 或 &gt;)


如題:
比如:在string.xml中定義如下一個字符串,
<string name="first">大家好,歡迎來到eoeandroid社區。welcome to here!</string>
我想以
大家好,歡迎來到eoeandroid社區。
welcome to here!
兩行的形式輸出,如何做?加\n,看下面:
<string name="hello">大家好,歡迎來到eoeandroid社區。\nwelcome to here!</string>



android中的空格編碼 string.xml前後加空格的技巧
<string name="space">&#160;&#160;&#160;&#160;我來看空格</string>
&#160; 這個就代表着空格


1. 遇到如下錯誤的時候說明你需要在單引號簽名加轉義字符(\):
Description Resource Path Location Type error: Apostrophe not preceded by \ (in Search' Titles) strings.xml
只要將定義的字符串中的單引號('), 修改为(\')即可

2. 變量文本格式(%s)提示:
Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false"
attribute?
- error: Unexpected end tag string
這是由於新的SDK(雖然從沒用過老的)采用了新版本的aapt(Android項目編譯器), 這個版本的aapt編譯起來會比老版本更加的嚴格, 在Android最新的開發文檔中描述String的部分,已經說明了如何去設置 %s 等符號, 可以點擊去看.
簡單解决方法就是:把%s之類的變量格式替換成%1$s, %1表示第一個位置的變量, $s表示为字符串類型
例如:

<string name="welcome_messages">Your First Var is %1$s! You Second Var is %2$d.</string>

posted @ 2013-07-07 01:28  日光之下无新事  阅读(388)  评论(0编辑  收藏  举报