我在某处读到如何在 XML 文档中使用变量。他们说这很简单,我想也是如此。我在 Android strings.xml 文件中成功地使用了它。我一整天都这样使用它,直到突然 android 停止解析它并停止将它视为变量。
我这样使用它:
<resources>
<string name="some_string">string1</string>
<string name="another_string"> {$some_string} trolololo </string>
</resources>
在java中通过以下方式访问它: getApplicationContext().getString(R.strings.another_string);
getApplicationContext().getString(R.strings.another_string);
在输出中我曾经接收如下字符串:
string1 trolololo
现在我只收到:
{$some_string} trolololo
有谁知道出了什么问题吗?我知道 Android 的 XML 可能与标准 XML 不同,但它曾经有效。噢......感谢您的任何建议。
假设您想将字符串值作为参数传递给another_string
那么你的字符串格式不正确,无法接收该参数,如果你尝试使用它,你的输出将是{$some_string} trolololo
.
如果您需要使用以下方式格式化字符串字符串.format(字符串,
目的...),那么您可以通过将格式参数放入
字符串资源。
<resources>
<string name="some_string">string1</string>
<string name="another_string">%1$s trolololo</string>
</resources>
现在您可以使用应用程序中的参数来格式化字符串,如下所示:
String arg = "It works!";
String testString = String.format(getResources().getString(R.string.another_string), arg);
Log.i("ARG", "another_string = " + testString);
这样做的输出字符串将是another_string = It works! trolololo
.
看看Android Developers官方文档,here http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)