JJD*_*JJD 32 android plural android-xml android-resources
我使用复数来编译Android应用程序的数量字符串.我完全按照教程中的内容进行操作:
res.getQuantityString(
R.plurals.number_of_comments, commentsCount, commentsCount);
Run Code Online (Sandbox Code Playgroud)
这是复数的定义:
<?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>
Run Code Online (Sandbox Code Playgroud)
有趣的是,输出字符串与我所定义的一样奇怪:
commentsCount = 0 => "0 comments"
commentsCount = 1 => "One comment"
commentsCount = 2 => "2 comments"
Run Code Online (Sandbox Code Playgroud)
我想这是因为文档说明When the language requires special treatment of the number 0 (as in Arabic).了zero数量.有没有办法强迫我的定义?
Dal*_*mas 55
根据文件:
选择使用哪个字符串完全基于语法必要性.在英语中,即使数量为0,也会忽略零字符串,因为0在语法上与2不相同,或者除了1之外的任何其他数字("零书","一本书","两本书"和等等).
如果您仍希望将自定义字符串用于零,则可以在数量为零时加载其他字符串:
if (commentsCount == 0)
str = res.getString(R.string.number_of_comments_zero);
else
str = res.getQuantityString(R.plurals.number_of_comments, commentsCount, commentsCount);
Run Code Online (Sandbox Code Playgroud)