android中的Plurals(Quantity Strings)类型

From:http://blog.csdn.net/ltl451011/article/details/6947642

不同的语言对数量的定义有不同的语法规则。例如,在英语中,1个指的是一种特殊情况。其他时候我们说n个。这种单数和复数的情况非常的普通。但其他语言却有明显的区别。Android中设置了0,1,2,"很少",“许多”和“其他”做为数量表述。

对于给定的语言和数量,决定使用那种情况是非常的困难和复杂的。Android提供了getQuantityString()来帮助我们决定选择适当的资源。

语法:

<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln">
</span><span class="tag"><</span><a target=_blank href="http://developer.android.com/guide/topics/resources/string-resource.html#plurals-resources-element"><span class="tag">resources</span></a><span class="tag">></span><span class="pln">
    </span><span class="tag"><</span><a target=_blank href="http://developer.android.com/guide/topics/resources/string-resource.html#plurals-element"><span class="tag">plurals</span></a><span class="pln">
        </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"</span><em><span class="atv">plural_name</span></em><span class="atv">"</span><span class="tag">></span><span class="pln">
        </span><span class="tag"><</span><a target=_blank href="http://developer.android.com/guide/topics/resources/string-resource.html#plurals-item-element"><span class="tag">item</span></a><span class="pln">
            </span><span class="atn">quantity</span><span class="pun">=</span><span class="atv">["zero"</span><span class="pln"> | </span><span class="atv">"one"</span><span class="pln"> | </span><span class="atv">"two"</span><span class="pln"> | </span><span class="atv">"few"</span><span class="pln"> | </span><span class="atv">"many"</span><span class="pln"> | </span><span class="atv">"other"</span><span class="pln">]
            </span><span class="tag">></span><em><span class="pln">text_string</span></em><span class="tag"></item></span><span class="pln">
    </span><span class="tag"></plurals></span><span class="pln">
</span><span class="tag"></resources></span>

 

文件位置:res/values/filename.xml
如何引用:R.plurals.plural_name

例子:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<plurals name="numberOfSongsAvailable">
       
<item quantity="one">One song found.</item>
       
<item quantity="other">%d songs found.</item>
   
</plurals>
</resources>

你可能感兴趣的:(android,Android开发)