"Turn" Pro Android Learning Note (iv): Learn about Android resources (below)

Source: Internet
Author: User
Tags getmessage

Working with arbitrary XML files

The custom XML file is placed under res/xml/and can be r.xml.file_name to get a Xmlresourceparser object. The following is an example of an XML file:

<rootname= "Tom" ><--can also be <root>, this time using a parametric approach as an experiment-
<leaf>hello from an elementtest.</leaf>
<leaf>hello world!</leaf>
<cornersradius= "13DP"/>

The XML file is parsed by layer by Javacode, with the following code:

try{//Prevent exceptions due to XML file writing or parsing analysis mismatch during parsing
Xmlpullparser xpp =getresources (). GETXML (r.xml.rt_test);
Gets the object of the XML resource

int eventtype = Xpp.next ();Equivalent to Xpp.next (); int EventType =xpp.geteventtype (), and Next () returns the type directly
while (eventtype! = xmlpullparser.end_document) {//is handled differently depending on the type
Switch (eventtype) {
LOG.D ("xml", "***************start****************");
int count = Xpp.getattributecount ();
LOG.D ("xml", "Starttap:" + xpp.getname () + "attribute number" +count);
for (int i = 0; i < count; i + +) {///Handle the Parameters section of the Yy=zz in <xxxx yy=zz....> here, note that XML is allowed to be written in this way unless the schema of the custom XML is determined to be not pure in this manner
LOG.D ("xml", "Attribute" + i + ":" + xpp.getattributename (i) + "=" +xpp.getattributevalue (i));
LOG.D ("xml", "Endtap:" + xpp.getname ());
LOG.D ("xml", "Text:" + xpp.gettext ());
LOG.D ("xml", "eventtype=" + eventtype);
EventType = Xpp.next ();
LOG.D ("xml", "***************end****************");
}catch (Throwable t) {
LOG.E ("xml", "Testxmlget exception T:" +t.getmessage ());

More detailed code descriptions can be read from Android Learning notes (38): Resource resource (top), XML parsing (xmlpullparser). The results of the operation are as follows:

Working with Raw resources

Raw resources are located in the Res/raw directory and are different from other resources and packaged into the app's apk without being compiled. Raw files can be files in any format, such as audio, video, and so on. Read is and Java file read all the time, the difference is through R.raw. filename to get.

protected void Testraw () {
try{//io Read, access to exceptions
InputStream is =getresources (). Openrawresource (r.raw.rt_test);//Get 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"));

Working with Asset resources

The asset resource is located in the/assets directory, which is balanced in the/res directory, which is not under/res, so the associated ID is not formed in R.java, and the asset resource cannot be obtained by ID. /assets is the user directory of the app, similar to the ~/directory in Linux users. We placed the text file Tv_test.txt in the directory. The following is the code read.

protected void Testassets () {
Assetmanager am = getassets (); //through Assetmanager to get the files in the/assets directory
InputStream is =am.open("Vt_test.txt");//Do not use ID, directly through the file name to obtain, and such as "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 all methods under activity, such as Activity.getresource ().

Changes to resources and configurations

Resources help with localization, including adaptation languages, horizontal/vertical screens, screen sizes, and more. The default folder described earlier in Res is also available, and you can have a conditional-adapted folder that holds the same name as an XML file that has the same ID in R.java. Android according to the actual situation, select the appropriate folder in the XML description. For example:


When the device is a vertical screen, layout-port is preferred, followed by layout. The conditional folder becomes the alternate resources, with the condition that the-port in the example above becomes the configuration qualifiers. The format of this adaptive Conditional folder is: default resource folder name-condition 1[-condition 2[-condition n ...]. The specific configuration qualifiers can be in: http://developer.android.com/guide/topics/resources/providing-resources.html# Alternativeresources, the following list:

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, Wheel: Type of navigation device.
v3, v4, V7: API level.

These conditions are prioritized, in the default resource folder name-condition 1[-condition 2[-condition n ...]] The priority level in condition 1 must be higher than the priority of condition 2, and the priority for the condition 2 must be higher than the priority level of condition 3. The level of the condition is pre-programmed by Android, and the priority of each condition listed above is 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 a vertical screen with a language of US English (En-rus), the order in which Android looks for resources by ID is/res/values-en-rus,/res/values-en-port,/res/values-en,/res/ Values-port,/res/values.

RELATED Links: My Android development related articles

Transfer from http://blog.csdn.net/flowingflying/article/details/9149527

Turn Pro Android learning Note (iv): Learn about Android resources (bottom)

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.