Pro Android learning notes (4): Learn about Android Resources (2)

Source: Internet
Author: User

Process arbitrary XML files

The custom xml file is placed under res/xml/. You can use R. xml. file_name to obtain an XMLResourceParser object. The following is an example of an xml file:

<Rootname = "tom"> <-- or <root>. This experiment uses parameters as an example. -->
<Leaf> Hello from an elementtest. </leaf>
<Leaf> Hello World! </Leaf>
<Cornersradius = "13dp"/>
</Root>

Use javacode to parse xml files layer by layer. The Code is as follows:

Try {// prevent exceptions caused by XML file writing or parsing and analysis mismatch during parsing
Xmlpullparser xpp = getresources (). getxml (R. xml. rt_test);
// Get the object of the XML Resource

Int eventtype = xpp. Next ();// Equivalent to xpp. next (); int eventType = xpp. getEventType (), while next () directly returns the type
While (eventType! = XmlPullParser. END_DOCUMENT) {// process data according to the type
Switch (eventtype ){
CaseXmlpullparser. start_document:
Log. D ("XML ", "*************** start ****************");
Break;
CaseXmlpullparser. start_tag:
Int COUNT = xpp. getattributecount ();
Log. D ("XML", "starttap:" + xpp. getname () + "attribute number" + count );
For (INT I = 0; I <count; I ++ ){// Process <xxxx yy = ZZ...> In the parameter section of YY = ZZ, note that XML is allowed to be written in this way, unless it is determined that the custom XML schema is not pure in this way
Log. d ("xml", "Attribute" + I + ":" + xpp. getAttributeName (I) + "=" + xpp. getAttributeValue (I ));
}
Break;
CaseXmlpullparser. end_tag:
Log. d ("xml", "endtap:" + xpp. getName ());
Break;
CaseXmlpullparser. Text:
Log. d ("xml", "text:" + xpp. getText ());
Break;
Default:
Log. d ("xml", "eventType =" + eventType );
Break;
}
Eventtype = xpp. Next ();
}
Log. d ("xml ", "*************** end ****************");
} Catch (throwable t ){ 
Log. e ("xml", "testXMLget exception t:" + t. getMessage ());
}

For more detailed code instructions, read the Android learning notes (): resource (I) and XML parsing (XmlPullParser ). The running result is as follows:

Raw resource processing

Raw resources are located in the res/raw directory. Unlike other resources, raw resources are packaged into the application apk without compilation. Raw files can be audio files, videos, and other files in any format. Reading is always the same as reading java files. The difference is that through R. raw.Filename.

Protected void testRaw (){
Try {// io read, get exceptions
Inputstream is = getresources (). openrawresource (R. raw. rt_test); // Obtain raw resources
Readtextfile (is );
Is. Close ();
} Catch (throwable t ){
Log. E ("Raw", "testraw error:" + T. getmessage ());
}
}

Private void readTextFile (InputStream is) throwsIOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
Int ch = is. read ();
While (ch! =-1 ){
Baos. write (ch );
Ch = is. read ();
}
Log. d ("raw", baos. toString ());
// Log. d ("raw", newString (baos. toByteArray (), "GB2312 "));
}

Process Asset resources

The asset resource is located in the/assets Directory, which is balanced under the/res directory, that is, it is not under/res, so it is not in R. in java, related IDs are formed, and asset resources cannot be obtained through IDs. /Assets is the application user directory, similar ~ /Directory. We have stored the tutorial file TV _test.txt in the directory. The following is the read code.

Protected void testAssets (){
Try {
AssetManager am = getAssets (); // Use AssetManager to obtain files in the/assets Directory
InputStream is = am. open("Vt_test.txt"); // No ID is used, which is obtained directly by file name, for example, "subbir/vt_test2.txt"
ReadTextFile (is );
Is. close ();
} Catch (Throwable t ){
Log. e ("asset", "testAsset Resource error:" + t. getMessage ());
}
}

Note that getResource () and getAsset () are both methods under activity, such as activity. getResource ().

Resource and configuration changes

Resources help localization, including adaptation languages, horizontal/vertical screens, screen sizes, and so on. The default folder described earlier in res can also have conditional adaptation-based folders that store XML files with the same name. These folders have the same ID in R. java. Android selects the XML description in the appropriate folder based on the actual situation. For example:

\ Res \Layout\ Main_layout.xml
\ Res \Layout-port\ Main_layout.xml
\ Res \Layout-land\ Main_layout.xml

When the device is a portrait screen, layout-port is preferred, followed by layout. The folder with the condition becomes alterate resources. The-port in the preceding example becomes configuration qualifiers. The format of this folder with adaptation conditions is:Default resource folder name-condition 1 [-condition 2 [-condition N...]. Specific configuration
Qualifiers can be:

MccAAA: AAA is the mobile country code.
MncAAA: AAA is the carrier/network code.
En-rUS: Language and region.
Sw <N> dp, w <N> dp, h <N> dp: Smallest width, available width, available height (since API 13 ).
Small, normal, large, xlarge: Screen size.
Long, notlong: Screen type.
Port, land: Portrait or landscape.
Car, desk: Type of docking.
Night, notnight: Night or day.
Ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi: Screen density.
Notouch, stylus, finger: Kind of screen.
Keysexposed, keyssoft, keyshidden: Kind of keyboard.
Nokeys, qwerty, 12key: Number of keys.
Navexposed, navhidden: Navigation keys hidden or exposed.
Nonav, dpad, trackball, and wheel: Type of navigation device.
V3, v4, v7: API level.

These conditions have a priority. In the default resource folder name-condition 1 [-condition 2 [-condition N...] the priority level of condition 1 must be higher than that of condition 2. Similarly, the priority level of condition 2 must be higher than that of Condition 3. The condition level is pre-set by Android. The priority level of each condition listed above is from high to low. For example, there are several folders:/res/values,/res/values-en,/res/values-en-rUS,/res/values-port, /res/values-en-port. For vertical screens in the United States English (en-rUS) language, Android searches for resources by ID in the order of/res/values-en-rUS, /res/values-en-port,/res/values-en,/res/values-port,/res/values.

Related links:
My Android development articles

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.