组织 Strings.xml
我正在制作一个 Android 应用程序,因为我刚刚开始,所以我想尝试获取最有组织的代码/资源。到目前为止,在我的 strings.xml 文件中,我有这样的内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="stop">Stop</string>
<string name="start">Start</string>
<string name="preferences">Preferences</string>
<string name="back">Back</string>
</resources>
除 app_name 之外的所有字符串都在选项菜单中使用。但由于我将添加更多的字符串,我认为这样做可能会更好:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
这是最好的方法还是我应该使用另一个系统?
I'm making an android app and since I've just started I want to try get the most organised code/resources. In my strings.xml file so far I have this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="stop">Stop</string>
<string name="start">Start</string>
<string name="preferences">Preferences</string>
<string name="back">Back</string>
</resources>
All of the strings except app_name are used in an options menu. But since I will be adding much more strings I was thinking that it might be better to do something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
Is it the best way or should I use another system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于字符串的使用位置。如果“stop”除了在菜单中之外不会在任何地方使用,那么将其称为“menu_stop”是一个好主意。如果它会在各处使用,那么它应该被称为“停止”。
此外,XML 注释对于组织资源也非常有用。
您发现有大量的字符串资源,您可能需要将它们分成不同的 xml 文件:menu_strings.xml、dialog_strings.xml 等
。
最后,如果
It depends on where the strings will be used. If "stop" will never be used anywhere but in a menu, calling it "menu_stop" is a good idea. If it'll be used all over the place then it should just be called "stop".
Also, XML comments are very useful for organizing resources.
Finally, if you find you have tons and tons of string resources you may want to go so far as to separate them into different xml files: menu_strings.xml, dialog_strings.xml, etc.
menu_strings.xml
dialog_strings.xml
这确实是一个主观问题。您应该使用您认为更容易处理的任何东西。当我使用布局和可绘制对象(例如,button_x、ninepatch_x、icon_x 等)时,我当然会使用第二种类型的命名,只是因为它使它们彼此相邻,并且更容易通过 Content Assist 快速缩小范围。在 XML 中,您可以使用注释将它们组合在一起,并添加空白,任何可以让您更轻松、快速地找到所需内容的内容。
This is kind of a subjective question, really. You should use whatever you find easier to handle. I certainly do the second type of naming when I'm using layouts and drawables (e.g. button_x, ninepatch_x, icon_x, etc.), just because it keeps them next to each other, and is easier to narrow down quickly with Content Assist. In XML, you can use comments to group them together, and add white space, just anything that makes it easier for you to find what you need, and quickly.