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
format statement in a string resource file
提问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 %s
or %d
.
您不需要formatted="false"
在您的 XML 中使用。您只需要使用完全限定的字符串格式标记 - %[POSITION]$[TYPE]
(其中[POSITION]
是属性位置,[TYPE]
是变量类型),而不是短版本,例如%s
or %d
。
Quote from Android Docs: String Formatting and Styling:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments:
%1$s
is a string and%2$d
is 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.xml
define 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.)
(与LocalPCGuy或Giovanny 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$s
is a string and%2$d
is 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);