我可以在 string.xml 的字符串资源中使用字符串资源吗?
正如标题所示,我的问题是我不知道是否/如何可以在 string.xml 中的字符串资源中使用字符串资源。
<string name="projectname">Include Test</string>
<string name="about">@string/projectname is to test if you can use a String Resource in a String Resource</string>
我的问题是是否有人知道我是否可以写,
<string name="identifier">@string/other_identifier</string>
因为 Eclispe 说
error: Error: No resource found that matches the given name (at 'other_identifier' with value '@string/other_identifier eingeben...').
As the title says, my problem is that I don't know If/How I can use a String Resource in a String Resource in string.xml.
<string name="projectname">Include Test</string>
<string name="about">@string/projectname is to test if you can use a String Resource in a String Resource</string>
My question is if anyone knows if I can write
<string name="identifier">@string/other_identifier</string>
because Eclispe says
error: Error: No resource found that matches the given name (at 'other_identifier' with value '@string/other_identifier eingeben...').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您最好在运行时构造这些
String
。在String.format
的帮助下将不同的String
资源组合在一起。更多详细信息:http://developer.android.com/guide/主题/资源/string-resource.html#FormattingAndStyling
You are better off constructing these
String
s at runtime. Combine differentString
resources together with the help ofString.format
.Further details: http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
不,我认为这是不可能的。
No, I don't think that is possible.
编辑:实际上这个解决方案,使用自定义
ENTITY
条目是很多更好。以下内容应该是开箱即用的:
我刚刚使用 API 级别 8 进行了尝试,效果很好。另请参阅 https://stackoverflow.com/a/6378421/786434
能够在其他单词中交叉引用字符串,您可以自己创建一个小辅助方法:
Edit: Actually this solution, using custom
ENTITY
entries is much nicer.The following should work out of the box:
I just tried it with API Level 8 and it works fine. See also https://stackoverflow.com/a/6378421/786434
To be able to cross-reference strings within other words, you can create yourself a small helper method:
在 xml 中插入常用字符串(例如应用程序名称)而不使用 Java 代码的好方法此处:
A nice way to insert a frequently used string (e.g. app name) in xml without using Java code here:
是的,您可以 :) 虽然不是原生的,但您可以使用这个小型库,它允许您在构建时解析这些占位符,因此您无需添加任何 Java/Kotlin 代码即可使其工作。
根据您的示例,您必须像这样设置字符串:
然后插件将负责创建以下内容:
更多信息在这里:https://github.com/LikeTheSalad/android-stem
免责声明:我是该库的作者。
Yes you can :) Not natively though, but you can use this small library that allows you to resolve these placeholders at buildtime, so you won't have to add any Java/Kotlin code to make it work.
Based on your example, you'd have to set up your strings like this:
And then the plugin will take care of creating the following:
More info here: https://github.com/LikeTheSalad/android-stem
Disclaimer: I'm the author of that library.