我使用plurals来为Android应用程序编译数量字符串。我完全遵循在教程中可以找到的内容:
res.getQuantityString(
R.plurals.number_of_comments, commentsCount, commentsCount);
这是复数的定义:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="number_of_comments">
<item quantity="zero">No comments</item>
<item quantity="one">One comment</item>
<item quantity="other">%d comments</item>
</plurals>
</resources>
足够有趣的是,输出字符串对我的定义很奇怪:
commentsCount = 0 => "0 comments"
commentsCount = 1 => "One comment"
commentsCount = 2 => "2 comments"
我猜这是因为docs声明
When the language requires special treatment of the number 0 (as in Arabic).
数量为zero
。有什么办法可以强求我的定义吗? 最佳答案
根据the documentation:
如果仍然要将自定义字符串用作零,则可以在数量为零时加载另一个字符串:
if (commentsCount == 0)
str = res.getString(R.string.number_of_comments_zero);
else
str = res.getQuantityString(R.plurals.number_of_comments, commentsCount, commentsCount);