Android 字符串资源文件中的格式语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12627457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:15:18  来源:igfitidea点击:

format statement in a string resource file

androidandroid-resources

提问by CocoNess

I have strings defined in the usual strings.xml Resource file like this:

我在通常的 strings.xml 资源文件中定义了这样的字符串:

<string name="hello_world"> HELLO</string>

Is it possible to define format strings such as the one below

是否可以定义如下格式字符串

 result_str = String.format("Amount: %.2f  for %d days ",  var1, var2);

in the strings.xml resource file?

在strings.xml 资源文件中?

I tried escaping the special characters but its not working.

我尝试转义特殊字符,但它不起作用。

回答by LocalPCGuy

You do not need to use formatted="false"in your XML. You just need to use fully qualified string format markers - %[POSITION]$[TYPE](where [POSITION]is the attribute position and [TYPE]is the variable type), rather than the short versions, for example %sor %d.

您不需要formatted="false"在您的 XML 中使用。您只需要使用完全限定的字符串格式标记 - %[POSITION]$[TYPE](其中[POSITION]是属性位置,[TYPE]是变量类型),而不是短版本,例如%sor %d

Quote from Android Docs: String Formatting and Styling:

引自Android 文档:字符串格式和样式

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$sis a string and %2$dis a decimal integer. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = res.getString(R.string.welcome_messages, username, mailCount);
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

在这个例子中,格式字符串有两个参数:%1$s是一个字符串和%2$d一个十进制整数。您可以使用应用程序中的参数格式化字符串,如下所示:

Resources res = getResources();
String text = res.getString(R.string.welcome_messages, username, mailCount);

回答by Sherif elKhatib

You should add formatted="false"to your string resource

您应该添加formatted="false"到您的字符串资源



Here is an example

这是一个例子

In your strings.xml:

在您的strings.xml

<string name="all" formatted="false">Amount: %.2f%n  for %d days</string>

In your code:

在您的代码中:

yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));

回答by Timo B?hr

Inside file strings.xmldefine a String resource like this:

内部文件strings.xml定义一个字符串资源是这样的:

<string name="string_to_format">Amount: %1$f  for %2$d days%3$s</string>

Inside your code (assume it inherits from Context) simply do the following:

在您的代码中(假设它继承自 Context)只需执行以下操作:

 String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);

(In comparison to the answer from LocalPCGuyor Giovanny Farto M.the String.format method is not needed.)

(与LocalPCGuyGiovanny Farto M的答案相比,不需要 String.format 方法。)

回答by Giovanny Farto M.

Quote from Android Docs:

引用自Android 文档

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$sis a string and %2$dis a decimal number. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

如果您需要使用 格式化您的字符串String.format(String, Object...),那么您可以通过将您的格式参数放在字符串资源中来实现。例如,使用以下资源:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

在此示例中,格式字符串有两个参数:%1$s是字符串和%2$d是十进制数。您可以使用应用程序中的参数格式化字符串,如下所示:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);