问题场景
老塞审代码的时候看到这样的资源问这个为什么要用if...else...处理,不用Quantity Strings做,好早之前看过一次原因,突然被问起竟然又忘记了。。。这次写下来好了。
<string name="this_pic">这张图片</string>
<string name="these_pics>这些图片</string>
Demo
在strings.xml中添加
<plurals name="pic">
<item quantity="one">这张图片</item>
<item quantity="other">这些图片</item>
</plurals>
在.java中读值并打印log
Log.v(TAG, getResources().getQuantityString(R.plurals.pic, 1));
结果:
当系统语言为中文时,log打印是 ------ 这些图片
当系统语言为英文时,log打印是 ------ 这张图片
为什么
Android Developer中的这段文字做出了解释
Although historically called "quantity strings" (and still called that in API), quantity strings should only be used for plurals. It would be a mistake to use quantity strings to implement something like Gmail's "Inbox" versus "Inbox (12)" when there are unread messages, for example. It might seem convenient to use quantity strings instead of an if statement, but it's important to note that some languages (such as Chinese) don't make these grammatical distinctions at all, so you'll always get the other string.
即,Quantity Strings应该是用来做复数的处理,而不是做区分数量的处理。比如,中文中没有复数的概念,都会返回other选项配置的string。
网友评论