问题
I love to put simple text directly on the xml file without declaring a string first. It is easier, more simple and less messy. However, it always has a warning badge whenever I do so.
What if I have a dozens of hardcoded string in my xml file without concerning the warning? Will I get into a trouble?
thanks in advance.
回答1:
No, you will not get into trouble, but using @string/yourString
in your xml will be a good practice and it will make multi-language support easier.
回答2:
When you use hard coded strings in both you java code and xml file your application directly writes these strings on RAM. But when you declare them as string resources it will not be written on RAM when application launches but they are written on RAM when application needs to use them. You can declare one billion strings in resources and you can still have a light weight RAM friendly Android application.
回答3:
There will not be any trouble with that. But using string.xml
to put text in your app is considered as a good programming practice. Because suppose you want to the same text in more than one places in your app. It will be difficult to put the text every time hard coded, especially if the text is somewhat large. Putting the text to a single file means that you have access to the same text from anywhere in you app.
回答4:
Your company was just bought out and now you get to read over thousands of lines of code and changer the old name to the new because somebody hard coded strings. You want to internationalize your code so it speaks Spanish easy.just make a new strings.xml only name it strings_es.xml the phone itself is set Spanish of course you would ttranslate but you are done.I can't read the strings cause this phone is so tiny, maybe you could give short descriptions for small phones. Read up on mvc good luck
回答5:
Nothing to worry about those warnings. Maybe the android developer thought it is not a good practice.
But if your application might support multi language one day, using @string/mystring is the best practice.
回答6:
This is in reference to some comments regarding performance (@PankajKumar, @TechEnd, @Ken Y-N):
According to the Google documentation, "String is compiled resource datatype: Resource pointer to a String." (Link: http://developer.android.com/guide/topics/resources/string-resource.html#String)
So, i think this will increase performance by decreasing the memory footprint, although the difference might not be noticeable.
This is because, when we declare a string in the XML file, a pointer to that resource is maintained. Whenever we need to reuse that resource again somewhere else, we just get a pointer to the existing String , thereby saving memory in allocating a new memory for the same String. This is in contrast to using hard coded strings, for which memory will allocated every-time for the same String resource.
Hope this helps. Comments are welcome.
Regards, MM.
来源:https://stackoverflow.com/questions/12634211/whats-wrong-with-hardcoded-string-in-android-xml-file