[英]How to use integer resources within string resources in android
如何在我的 strings.xml 文件中使用我的 integers.xml 文件中存储的一些整数。
例如:
我有<integer name="some_integer">5</integer>
我想在我的 strings.xml 文件中使用它:
<string name="some_string">This is my string num @integers/some_integer in a row</string>
显然我的方法不够好,所以我需要一点帮助。 我相信有一种可能的解决方案,我只是不知道正确的解决方案。
感谢所有的帮助!
简短的版本是你不能像这样混合资源,但你可以在 Java 中使用:
getResources.getString(R.string.some_string,
getResources.getInteger(R.integer.some_integer)
);
然后在你的字符串 XML
<string name="some_string">This is my string num %d in a row</string>
这样%d
将被您在getString
中传递的整数替换
其他人说你的方法不起作用。 这是面对类似问题的旧答案:
<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);
请参阅此参考资料:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.