Why do I need to store the text that appears in the application separately in the String.xml file?
One: For internationalization, when the need for internationalization, only need to provide a string.xml file, the inside of the man information are modified to the corresponding language (such as English), and then run the program, The Android operating system will automatically select the appropriate String.xml file based on the locale and country of the user's phone, and the phone interface will be displayed in English. It is very convenient to do so internationally.
Second: In order to reduce the volume of the application, reduce data redundancy. Suppose in the application to use the "we have been working" this paragraph 1000 times, if each use of the words directly written, so that down the program will have 70,000 words, these 70,000 words accounted for 136KB space. And because of the limited resources of the mobile phone, its CPU processing capacity and memory is very limited, 136KB of mobile phone memory is a small space, we do mobile applications is sure to remember "can save memory on the memory." If these words are defined in String.xml, the text is referenced by the resources class at each use, and only 14B is used. Therefore, it is very effective to reduce the application volume effect. Of course we may not use so much text information during development, but as a mobile app developer, we must develop good programming habits.
There are several different places to get the values in the String.xml file.
1. In XML files such as Androidmanifest.xml and layout:
android:text= "@string/resource_name"
2. In the activity:
Method One: This.getstring (r.string.resource_name);
Method Two: Getresources (). getString (R.string.resource_name);
3. In other Java files (must have context or pplication)
Method One: Context.getstring (r.string.resource_name);
Method Two: Application.getstring (r.string.resource_name);
Use of string.xml files in Android
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);
Or:
String Appname= (String) this.getresources (). getString (R.string.app_name);
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>
Assets Access to Folder resources
The files inside the assets folder are kept in the original file format and need to be read by Assetmanager in the form of a byte stream.
1. First call Getassets () in the activity to get the Assetmanager reference.
2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and the access mode to get the input stream inputstream.
3. Then you read the file with the InputStream of the open file, and remember to Inputstream.close () after the read is done.
4. Call Assetmanager.close () to close Assetmanager.
It is important to note that files from resources and assets can only be read and cannot be written.
The following is read from the raw file:
Public String Getfromraw () {
try {
InputStreamReader Inputreader = new InputStreamReader (Getresources (). Openrawresource (R.raw.test1));
bufferedreader bufreader = new BufferedReader (inputreader);
String line= "";
String result= "";
While (line = Bufreader.readline ()) = null) /c11>
result + = line;
return Result;
} catch (Exception e) {
e.printStackTrace ();
} < /c0>
}
The following is read directly from assets
Public String getfromassets (String fileName) {
try {
inputstreamreader inputreader = new InputStreamReader (Getresources (). g Etassets (). open (FileName);
bufferedreader bufreader = new BufferedReader (inputreader);
String line= "";
String result= "";
While (line = Bufreader.readline ()) = null) /c11>
result + = line;
return Result;
} catch (Exception e) {
e.printstacktrace ();
}
}
Of course, if you want to get the memory stream, you can return to the memory stream directly!
Transferred from: http://blog.sina.com.cn/s/blog_618199e601011bst.html
Android Gets the value of String.xml (GO)