【发布时间】:2016-09-26 05:39:29
【问题描述】:
我的目标是显示“1 星”“2.5 星”“3 星”之类的文字。
对于像1.0, 2.0 etc. 这样的浮点值删除.0 但对于.5 的值显示浮点值2.5 3.5 etc.
这样可以用androidplurals吗?
我用过这种方法,但不起作用。 plurals.xml
<plurals name="hotel_card_rating_text">
<item quantity="zero">@string/text_zero</item>
<item quantity="one">@string/text_one</item>
<item quantity="two">@string/text_two</item>
<item quantity="few">@string/text_few</item>
<item quantity="many">@string/text_many</item>
<item quantity="other">@string/text_other</item>
</plurals>
strings.xml
<resources>
<string name="text_zero">%.1f stars</string>
<string name="text_one">1 star</string>
<string name="text_two">%.1f stars</string>
<string name="text_few">%.1f stars</string>
<string name="text_many">%.1f stars</string>
<string name="text_other">%.1f stars</string>
</resources>
测试代码
DecimalFormat format = new DecimalFormat();
float rating = getRating();
getStarText().setText(getContext().getResources().getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
}
【问题讨论】:
标签: android