组织 Strings.xml

发布于 2024-10-10 00:49:47 字数 938 浏览 3 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2024-10-17 00:49:47

这取决于字符串的使用位置。如果“stop”除了在菜单中之外不会在任何地方使用,那么将其称为“menu_stop”是一个好主意。如果它会在各处使用,那么它应该被称为“停止”。

此外,XML 注释对于组织资源也非常有用。

<resources>
    <string name="app_name">GameController</string>

    <!-- Menu Strings -->
    <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>

您发现有大量的字符串资源,您可能需要将它们分成不同的 xml 文件:menu_strings.xml、dialog_strings.xml 等

<resources>
    <!-- Menu Strings -->
    <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>

最后,如果

<resources>
    <string name="dialog_cancel_yes">Yes, cancel.</string>
    <string name="dialog_cancel_no">No, do not cancel.</string>
</resources>

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.

<resources>
    <string name="app_name">GameController</string>

    <!-- Menu Strings -->
    <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>

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

<resources>
    <!-- Menu Strings -->
    <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>

dialog_strings.xml

<resources>
    <string name="dialog_cancel_yes">Yes, cancel.</string>
    <string name="dialog_cancel_no">No, do not cancel.</string>
</resources>
楠木可依 2024-10-17 00:49:47

这确实是一个主观问题。您应该使用您认为更容易处理的任何东西。当我使用布局和可绘制对象(例如,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文