Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

android - Organizing Strings.xml

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...