关于Android strings.xml-你应该了解的几个原则

ki484923 7年前
   <p>但是说不定什么时候你使用不同的string了,这时你就需要重新创建两个新的string,而且还要修改java代码。如果一开始你就使用两个string的话,你需要修改的就只有strings.xml文件。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/bd00fdd72bfdbff5e5fd7227a42150da.png"></p>    <p>res/values/strings.xml</p>    <p>2. 你永远不知道你的应用要支持哪些语言。在某一种语言中-或许你可以在不同的上下文中使用同一个词语,但是在另一种语言中很可能不同的上下文需要不同的词语。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/350b85b22f0c7275de3ad51c8cc6c3e8.png"></p>    <p>res/values/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/bb6d0fff12cd7c0ceb5b30fd6ec6248d.png"></p>    <p>res/values-UA/strings.xml</p>    <p>注意这里英语版本的strings.xml中,R.string.download_file_yes 和 R.string.terms_of_use_yes两个string都是同一个单词- “Yes”。</p>    <p>但是在乌克兰语版本中R.string.download_file_yes用的是 “Гаразд”,而 R.string.terms_of_use_yes用的是“Так”。</p>    <h3>分离</h3>    <p>同一页面的string用前缀和注释分离</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/fa5f6115d2b9f33547ef0e75bf219980.png"></p>    <p>res/values/strings.xml</p>    <p>为每个页面创建单独的 strings.xml 文件</p>    <p>如果你想为每个页面创建一个 strings.xml文件,可以这样命名:settings-strings.xml, profile-strings.xml。但是一个app通常有10到20个页面。所以每一个语言目录下都会有10到20个strings.xml文件,个人认为这带来的是混乱。</p>    <h3>Format格式化</h3>    <p>使用 Resources#getString(int id, Object… formatArgs) 来格式化 strings</p>    <p>永远不要使用+号来拼接字符串,因为不同的语言语序是不同的。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c1d05cd7c1a586fc73357cbf2317c0f5.png"></p>    <p>res/values/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/18486d851c7354b0f10689b9512eac00.png"></p>    <p>java code</p>    <p>正确的方式是使用 <a href="/misc/goto?guid=4959740111345199585" rel="nofollow,noindex">Resources#getString(int id, Object… formatArgs)</a> 。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/62c4ecb07c212db62bd2803687ff2b18.png"></p>    <p>res/values/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/e5783081a512b2df558af853cbaa2efa.png"></p>    <p>res/values-UA/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/7232327ea083613c575c7bc7450c1da1.png"></p>    <p>java code</p>    <h3>复数</h3>    <p>使用 Resources#getQuantityString (int id, int quantity) 来处理复数</p>    <p>永远不要在java代码中解决复数问题,因为不同语言中复数的语法规则是不同的。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c8fbfbb890eb409b1f57d58d96e03505.png"></p>    <p>res/values/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/f98ca5528748b31febf72716ae8be4a4.png"></p>    <p>java code</p>    <p>正确的方式是使用 <a href="/misc/goto?guid=4959740111436285237" rel="nofollow,noindex">Resources#getQuantityString (int id, int quantity</a> )。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/10fac32fc6e67040ba738faa3e92bd46.png"></p>    <p>res/values/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/fa97e4313ce29dd906c2e6a6a2244e80.png"></p>    <p>注:关于 getQuantityString方法请看: <a href="/misc/goto?guid=4959740111507236559" rel="nofollow,noindex">Android Quantity Strings 的 getQuantityString方法取值问题</a>  </p>    <p>java code</p>    <h3>词语高亮显示</h3>    <p>使用 html文本来高亮静态词语</p>    <p>如果你想改变TextView中的某些单词的颜色- <a href="/misc/goto?guid=4959740111602640823" rel="nofollow,noindex">ForegroundColorSpan</a> 并不总是最佳选择,因为它的高亮是通过索引来完成的,而且在多语言下不安全。最好在 strings.xml文件中使用html的font color标签。</p>    <p>假设你有一个“Discover and play games.” 的文本。你想让单词 “Discover” 和 “play” 显示成蓝色。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/2ceaefe866a80f348ae2d6d764b9abb9.png"></p>    <p>res/values/strings.xml</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/5c1faa6b464fc106591fabd650a1fe06.png"></p>    <p>java code</p>    <p>注:其实本文的很多观点和这篇文章不谋而合。</p>    <p> </p>    <p>来自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0303/7179.html</p>    <p> </p>