简体   繁体   English

如何在android中的字符串资源中使用整数资源

[英]How to use integer resources within string resources in android

How can I use some integer that I have stored in my integers.xml file in my strings.xml file.如何在我的 strings.xml 文件中使用我的 integers.xml 文件中存储的一些整数。

For example:例如:

I have <integer name="some_integer">5</integer> and I would like to use this in my strings.xml file:我有<integer name="some_integer">5</integer>我想在我的 strings.xml 文件中使用它:

<string name="some_string">This is my string num @integers/some_integer in a row</string>

Apparently my way is not good enough so I need a little help please.显然我的方法不够好,所以我需要一点帮助。 I belive there is a possible solutions I just don't know the right one.我相信有一种可能的解决方案,我只是不知道正确的解决方案。

Appreciate all the help!感谢所有的帮助!

short version is that you can't mix resources like this, but you can use in Java:简短的版本是你不能像这样混合资源,但你可以在 Java 中使用:

getResources.getString(R.string.some_string,
                       getResources.getInteger(R.integer.some_integer)
                      );

and then in your String XML然后在你的字符串 XML

<string name="some_string">This is my string num %d in a row</string>

that way the %d get replaced by the integer you pass in the getString这样%d将被您在getString中传递的整数替换

Others said your approach do not work.其他人说你的方法不起作用。 This is an old answer to face similar problem:这是面对类似问题的旧答案:

<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>

int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage);
String strMeatMsg = String.format(strMeatFormat, numPoundsMeat);

See this references:请参阅此参考资料:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM