Android strings.xml 中的参数是否可能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2397613/
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
Are parameters in strings.xml possible?
提问by dhesse
In my Android app I'am going to implement my strings with internationalization. I have a problem with the grammar and the way sentences build in different languages.
在我的 Android 应用程序中,我将通过国际化实现我的字符串。我的语法和句子在不同语言中的构建方式有问题。
For example:
例如:
"5 minutes ago" - English
"vor 5 Minuten" - German
“5 分钟前” - 英语
“vor 5 Minuten” - 德语
Can I do something like the following in strings.xml?
我可以在 strings.xml 中执行以下操作吗?
<string name="timeFormat">{0} minutes ago</string>
And then some magic like
然后是一些魔法
getString(R.id.timeFormat, dynamicTimeValue)
This behaviour would solve the other problem with different word orders as well.
这种行为也可以解决不同词序的另一个问题。
回答by Christopher Orr
Yes, just format your strings in the standard String.format()
way.
是的,只需以标准String.format()
方式格式化您的字符串。
See the method Context.getString(int, Object...)
and the Androidor JavaFormatter
documentation.
请参阅该方法Context.getString(int, Object...)
和Android或JavaFormatter
文档。
In your case, the string definition would be:
在您的情况下,字符串定义将是:
<string name="timeFormat">%1$d minutes ago</string>
回答by CONvid19
If you need two variables in the XML
, you can use:
如果在 中需要两个变量XML
,可以使用:
%1$d text... %2$d
or %1$s text... %2$s
for string variables.
%1$d text... %2$d
或%1$s text... %2$s
用于字符串变量。
Example :
例子 :
strings.xml
字符串.xml
<string name="notyet">Website %1$s isn\'t yet available, I\'m working on it, please wait %2$s more days</string>
activity.java
活动.java
String site = "site.tld";
String days = "11";
//Toast example
String notyet = getString(R.string.notyet, site, days);
Toast.makeText(getApplicationContext(), notyet, Toast.LENGTH_LONG).show();
回答by Gabriel Lucas
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);
If you wish more look at: http://developer.android.com/intl/pt-br/guide/topics/resources/string-resource.html#FormattingAndStyling
如果你想更多看看:http: //developer.android.com/intl/pt-br/guide/topics/resources/string-resource.html#FormattingAndStyling
回答by Divide
Note that for this particular application there's a standard library function, android.text.format.DateUtils.getRelativeTimeSpanString()
.
请注意,对于此特定应用程序,有一个标准库函数android.text.format.DateUtils.getRelativeTimeSpanString()
.
回答by Gorio
There is many ways to use it and i recomend you to see this documentation about String Format.
有很多方法可以使用它,我建议您查看有关字符串格式的文档。
http://developer.android.com/intl/pt-br/reference/java/util/Formatter.html
http://developer.android.com/intl/pt-br/reference/java/util/Formatter.html
But, if you need only one variable, you'll need to use %[type]where [type]could be any Flag (see Flag types inside site above). (i.e. "My name is %s" or to set my name UPPERCASE, use this "My name is %S")
但是,如果您只需要一个变量,则需要使用 % [type]其中[type]可以是任何标志(请参阅上面站点内的标志类型)。(即“我的名字是%s”或将我的名字设置为大写,使用这个“我的名字是%S”)
<string name="welcome_messages">Hello, %1$S! You have %2$d new message(s) and your quote is %3$.2f%%.</string>
Hello, ANDROID! You have 1 new message(s) and your quote is 80,50%.