Android多元化不起作用,需要帮助
题
我一直在尝试使用Android使用Clurals资源,但没有任何运气。
这是我的复数资源文件:
<?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()));
...但是文本视图中的文字仍然是 %d degrees
和 %d meters
.
有人知道发生了什么吗?我已经调试了代码和res.getQuantityString(...)调用正在返回一个值的字符串 %d degrees
或者 %d meters
. 。尽管当数量恰好是1时,它确实正确地评估了 1 degree
或者 1 meter
.
在此先感谢您的任何帮助!
关于celestialorb的问候。
解决方案
看来您需要两次指定计数,第一个用于确定要使用的字符串,第二个是替换为字符串的字符串。例如
Resources res = this.getResources();
int tv = this.mObject.temp_c.intValue();
tTemp.setText(res.getQuantityString(R.plurals.degrees, tv, tv));
至少到目前为止我的测试 xliff:g
不需要资源中的元素。
其他提示
Android“支持”通过使用R.Plurals实际上没有证件的r.plurals使用。潜入源代码显示您应该能够拥有以下可能的字符串版本:
- “零”
- “一”
- “很少”(正好2)
- “其他”(3及以上)
但是,我发现只有“一个”和“另一个”实际上起作用(尽管Android来源中使用了其他人!)。
要使用要宣布可复杂的字符串的复数,以与普通字符串资源相似的方式:
<resources>
<plurals name="match">
<!-- Case of one match -->
<item quantity="one">1 match</item>
<!-- Case of several matches -->
<item quantity="other">%d matches</item>
</plurals>
</resources>
然后,要在代码中实际使用它们,请使用与上述SuperFell相似的代码:
String text = getResources().getQuantityString(R.plurals.match, myIntValue, myIntValue);
myTextView.setText(text);
同样的问题!我想这只是文档中的一个缺陷。 “纯” getQuantitiyString(int, int)
方法确实只获得文本资源,而无需任何格式。正如Superfell所说:只需使用 getQuantityString(int, int, Object...)
方法并交出两次整数值。
我希望这与您相同的方式工作,但这根本没有!
PS:也许将答案视为正确的答案? ;-)
不隶属于 StackOverflow