如果我的strings.xml中有以下多个ressource:
<plurals name="item_shop">
<item quantity="zero">No item</item>
<item quantity="one">One item</item>
<item quantity="other">%d items</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法向用户显示结果:
textView.setText(getQuantityString(R.plurals.item_shop, quantity, quantity));
Run Code Online (Sandbox Code Playgroud)
它适用于1及以上,但如果数量为0,那么我看到"0项".是否只有阿拉伯语支持"零"值,正如文档似乎表明的那样?或者我错过了什么?
我试图在参考资料中使用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)
一种解决方法是将字符串分解为仅对字符串的最后部分使用复数并连接这两部分.但我试图尽可能避免这样做.
我试图在我最新的Android项目(SDK9)中使用复数.它始终以ResourcesNotFoundException结尾.但资源就在那里 - 绝对:
这是我的strings.xml的一部分:
<plurals name="count_files">
<item quantity="one">%d file</item>
<item quantity="other">%d files</item>
<item quantity="zero">%d files</item>
</plurals>
<plurals name="count_folders">
<item quantity="one">%d folder</item>
<item quantity="other">%d folders</item>
<item quantity="zero">%d folders</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)
这是使用它的部分:
textView.setText(
getResources().getQuantityString(R.plurals.count_folders, countfolders, countfolders)
+ ", "
+ getResources().getQuantityString(R.plurals.count_files, countfiles, countfiles));
Run Code Online (Sandbox Code Playgroud)
这是例外:
android.content.res.Resources$NotFoundException: Plural resource ID #0x7f050001 quantity=0 item=other
我必须保持多达15种不同的语言,因此我需要文本和复数的本地化.
知道我的代码有什么问题吗?
提前谢谢了.
我是Android开发的新手,面临管理Android资源的问题.我想用ImageView和TextView创建一个listView.
以下是我的实现工作正常,但实际上我想使用我之前创建的数组,如下所示:
int[] img = getResources().getIntArray(R.Array.img);
Run Code Online (Sandbox Code Playgroud)
package com.simplelistviewwithlistactivity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
public class ListActivityS extends ListActivity {
int[] img = { R.drawable.r1, R.drawable.r2, R.drawable.skycubemap1,
R.drawable.skycubemap1, R.drawable.skycubemap2,
R.drawable.skycubemap3, R.drawable.skycubemap4,
R.drawable.skycubemap5 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setDividerHeight(2);
getListView().setAdapter(new BindDataAdapter(this, img, item));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(item[position] + " is clicked."); …Run Code Online (Sandbox Code Playgroud) 在res/values/strings.xml,我有
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="days_amount">
<item quantity="one">%d day EN</item>
<item quantity="other">%d days EN</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
在res/values-fr/strings.xml,我有
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="days_amount">
<item quantity="one">%d jour FR</item>
<item quantity="other">%d jours FR</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
使用英语语言环境res.getQuantityString(R.plurals.days_amount, 0, 0)返回:
“0天”
根据英语规则,这是可以的(又名零数量 <=> 几个数量)。
但是对于法语语言环境,它返回:
“0 小时”
根据法国规则,这是不行的(又名零数量 <=> 单数)。
它应该回来了 "O jour"
法语规则:http : //www.unicode.org/cldr/charts/25/supplemental/language_plural_rules.html#fr
那么,这是一个错误,还是我做错了什么?
我开始遇到 android 复数形式,并且我对这个功能的可用性感到满意。我声明plurals.xml并尝试从我的代码中读取它们,但我得到了不正确的结果。
复数:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numbers">
<item quantity="zero">No comments</item>
<item quantity="one">%1$d comment.</item>
<item quantity="two">%1$d comments.</item>
<item quantity="few">%1$d comments.</item>
<item quantity="many">%1$d comments.</item>
<item quantity="other">%1$d comments.</item>
</plurals>
<plurals name="numberOfSongsAvailable">
<item quantity="zero">No song found.</item>
<item quantity="one">One song found.</item>
<item quantity="two">Two song found.</item>
<item quantity="other">Other songs found.</item>
</plurals>
</resources>
Run Code Online (Sandbox Code Playgroud)
活动 :
public class AndroidPlurals extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.plurals);
((ListView) findViewById(R.id.listView)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getPluralsList(10)));
Toast.makeText(this, getClass().getSimpleName(), Toast.LENGTH_LONG).show();
}
private String[] getPluralsList(int k) …Run Code Online (Sandbox Code Playgroud)