Android多元化不起作用,需要帮助

前端之家收集整理的这篇文章主要介绍了Android多元化不起作用,需要帮助前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图利用 Android的复数资源,但没有任何运气.

这是我的复数的资源文件

<?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        <plurals name="meters">
            <item quantity="one">1 meter</item>
            <item quantity="other">
                <xliff:g id="count">%d</xliff:g>
                meters
            </item>
        </plurals>
        <plurals name="degrees">
            <item quantity="one">1 degree</item>
            <item quantity="other">
                <xliff:g id="count">%d</xliff:g>
                degrees
            </item>
        </plurals>
    </resources>

…然后这里是我试图从我的资源中提取数量字符串时使用的代码

Resources res = this.getResources();
tTemp.setText(res.getQuantityString(R.plurals.degrees,this.mObject.temp_c.intValue()));

…但TextView中的文本仍然是%d度和%d米.

有人知道发生了什么吗?我调试了代码,res.getQuantityString(…)调用返回一个值为%d度或%d米的字符串.虽然当数量恰好为1时,它正确地评估为1度或1米.

提前感谢任何帮助!

问候,celestialorb.

解决方法@H_403_20@
看来,您需要指定两次计数,第一个用于确定要使用的字符串,第二个是替换为字符串的字符串.例如
Resources res = this.getResources();
int tv = this.mObject.temp_c.intValue();
tTemp.setText(res.getQuantityString(R.plurals.degrees,tv,tv));

至少在我的测试中,资源中的xliff:g元素是不需要的.

原文链接:https://www.f2er.com/android/311110.html

猜你在找的Android相关文章