問題場景
老塞審代碼的時候看到這樣的資源問這個為什么要用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));
結(jié)果:
當(dāng)系統(tǒng)語言為中文時,log打印是 ------ 這些圖片
當(dāng)系統(tǒng)語言為英文時,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應(yīng)該是用來做復(fù)數(shù)的處理,而不是做區(qū)分?jǐn)?shù)量的處理。比如,中文中沒有復(fù)數(shù)的概念,都會返回other選項配置的string。