我使用复数来编译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数量.有没有办法强迫我的定义?
我试图在参考资料中使用getQuantityString方法来检索基于android开发人员指南的数量字符串(复数)数量字符串(复数)
我得到的错误是
Error:(604) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
Error:(604) Found tag </item> where </plurals> is expected
Run Code Online (Sandbox Code Playgroud)
当我设置复数如下
<plurals name="productCount">
<item quantity="one" formatted="true">%1$d of %2$d product</item>
<item quantity="other" formatted="true">%1$d of %2$d products</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)
并尝试阅读如下
productIndexCountText.setText(getResources().getQuantityString(R.plurals.productCount, position, size));
Run Code Online (Sandbox Code Playgroud)
一种解决方法是将字符串分解为仅对字符串的最后部分使用复数并连接这两部分.但我试图尽可能避免这样做.