1. Getting strings and values in String.xml in the program
<?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. Defining a string array (Arrays.xml)
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string-array name= "Sports" >
<item> Football </item>
<item> Basketball </item>
<item> Taiji </item>
<item> Hockey </item>
</string-array>
</resources>
----Getresources (). Getstringarray (R.string.sports);
3. Define 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. Defining Dimensions (Dimens.xml)
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<dimen name= "Height" >80dip</dimen>
</resources>
---getresource (). Getdimension (R.string.height);
5. Define 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 should the text appearing in the application be stored separately in the String.xml text?
One: For internationalization, Android recommends that the text displayed on the screen be defined in the Strings.xml if it is to be internationalized in the future, such as our
The development of the application is intended for domestic users, of course, the use of Chinese on the screen, and now we want to let the application to the world, into the Japanese market, of course, need
To display Japanese on the phone screen, if you do not define the text message in the String.xml, you need to modify the contents of the program. But when we put all the screens
The text information appearing on the screen is stored in the String.xml file, only need to provide a string.xml file, the man information inside the change
When you run the program in Japanese, the Android operating system automatically selects the appropriate String.xml file based on the locale and country of the user's phone.
The phone interface will display Japanese. It is very convenient to do so internationally.
Second: In order to reduce the volume of the application, reduce data redundancy. Suppose you want to use the word "We've been working" 1000 times in the app, if we don't
"We have been trying to" define in the String.xml file, but to write these words directly on each use, so that there will be 70,000 characters in the program,
These 70,000 characters occupy 136KB of space. Due to the limited resources of the mobile phone, its CPU processing capacity and memory is very limited, 136KB to the memory of the mobile phone
is a small space, we do mobile applications are sure to remember, "can save memory to save memory." And if you define these words in String.xml,
Each use of the resources class to refer to the text, only occupy 14B, so it is very effective to reduce the application of volume effect. Of course we
may not be used in the development of such a lot of text information, but "not good small but not for the small," as a mobile app developer
, we must develop good programming habits.
Transferred from: http://blog.csdn.net/xuyide54321/article/details/7087848
"Go" use of string.xml files in Android