1. InProgramTo obtain the string and value in string. xml.
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello world, mainactivity! </String>
<String name = "app_name"> testexample01 </string>
</Resources>
Use in activity:
String appname = (string) This. getresources (). gettext (R. String. app_name );
Log. I ("test", "appname =" + appname );
Or:
String appname = (string) This. getresources (). getstring (R. String. app_name );
Log. I ("test", "appname =" + appname );
2. Define a String Array (arrays. XML)
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "Sports">
<Item> football </item>
<Item> basketball </item>
<Item> tai chi </item>
<Item> ice hockey </item>
</String-array>
</Resources>
---- Getresources (). getstringarray (R. String. Sports );
3. Define the color (colors. XML)
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color name = "black"> # ffffff </color>
</Resources>
--- Getresources (). getdrawable (R. String. Black );
--- Getresources (). getcolor (R. String. Black );
4. Define dimensions (dimens. XML)
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Dimen name = "height"> 80dip </dimen>
</Resources>
--- Getresource (). getdimension (R. String. Height );
5. Define a style (styles. XML)
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "sharptext">
<Item name = "Android: textsize"> 18sp </item>
<Item name = "Android: textcolor" >#000000 </item>
</Style>
</Resources>
6. Why do I need to store the text in the application in the string. xml file separately?
I: For internationalization, Android recommends defining the text displayed on the screen in strings. xml. If you want to internationalize it in the future, for example
The developed applications are intended for domestic users. Of course, Chinese characters are used on the screen. Now, we need to make the applications go global and enter the Japanese market.
To display Japanese on the mobile phone screen, if the text information is not defined in string. XML, You need to modify the program content. But when we put all the screens
All text information displayed on the screen is stored in the string. xml file. You only need to provide another string. xml file to modify all the man information in it.
When you run the program again, the Android operating system automatically selects the corresponding string. xml file based on the language environment and country of your mobile phone.
Japanese is displayed on the mobile phone interface. This is very convenient for internationalization.
II: To reduce the volume of applications and data redundancy. Assume that "we have been working hard" is used for 1000 times in the application. If we do not
"We have been trying to" define it in the string. xml file, but write these words directly at each usage. In this way, the program will have 70000 words,
These 70000 words account for KB of space. Due to limited mobile phone resources, the CPU processing capability and memory are very limited. KB for mobile phone memory
It is not a small space. We must remember to "save memory when saving memory" when making mobile apps ". If you define these words in string. XML
Each time we use the resources class to reference this text, it only occupies 14B, so it is very effective to reduce the application volume. Of course we
This may not be used in the development of mobile app developers.
We must develop good programming habits.