Seven typical cases of file operations on Android Resources (2)

Source: Internet
Author: User

Resources in Android refer to non-code parts, such as clips, audios, videos, and characters. Generally, we store native files in assets. For example, MP3 files cannot be directly accessed by Android programs. They must be read as binary streams through the AssetManager class. Layout files are generally stored in the res folder, such as anim, drawable, layout, values, xml, raw, and menu. These resources can be directly accessed through the R Resource class. Resources in assets are rarely used, and resources in res are often used.

Next we will continue to explain how to use these typical layout files:

4. Use original XML resources: if some original XML files are used in the project, we can define some XML files for the project.

1) definition and use of strings:

Resource location: res/xml/test. xml

Original XML file format:

Method for obtaining XML Resources: Resources. getXml ()

Reference XML Resource Method: in Java code: R. xml. xml_name

Case study: the basic idea of getting the original XML file is to use Resources. get the original XML file by getXml () and get the XmlResourcePaser object. Use this object to determine whether it is the beginning or end of a document, the beginning or the end of a tag, and traverse the XMl file by obtaining the attributes to access the content of the MXL file.

Xml file:





Core code in java:

Resources r = getResources ();
XmlResourceParser xrp = r. getXml (R. xml. person );
Try {
// If the path does not reach the end of the file, continue the loop.
While (xrp. getEventType ()! = XmlResourceParser. END_DOCUMENT ){
// If it is a start tag
If (xrp. getEventType () = XmlResourceParser. START_TAG ){
String name = xrp. getName ();
// Determine whether the tag name is equal to person
If (name. equals ("person ")){
Counter ++;
// Obtain the tag attribute and append it to StringBuilder.
Sb. append ("no." + counter + "user information: \ n ");
Sb. append ("name:" + xrp. getAttributeValue (0) + "\ n ");
Sb. append ("age:" + xrp. getAttributeValue (1) + "\ n ");
Sb. append ("gender:" + xrp. getAttributeValue (2) + "\ n ");
}
} Else if (xrp. getEventType () = XmlPullParser. END_TAG ){
} Else if (xrp. getEventType () = XmlPullParser. TEXT ){
}
Xrp. next ();
}
// Set StringBuilder to TextView text
MyTextView. setText (sb. toString ());
MyButton. setText ("exit ");
} Catch (XmlPullParserException e ){
E. printStackTrace ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}

}

5. Use drawables resources: some images or Color resources are mainly used to draw screens. There are three types: Bitmap File and Color Drawable) and NIne-Patch Image (NIne images ).

1) definition and use of drawable resources:

Resource location: res/drawable/file_name.jpg/file_name.png/file_name.gif

String obtaining method: Resources. getDrawable ()

Resource reference method: in Java code: R. drawable. file_name

In the string XML file: @ [package:] drawable/file_name

Case: myImageView = (ImageView) findViewById (R. id. mypic );

Resources r = getResources ();

Drawable d = r. getDrawable (R. drawable. moto );

MyImageView. setImageDrawable (d );

6. layout resources: layout resources are the most commonly used resources in Android. Android can define the layout of components on the screen in an XML file, this is a bit like the HTML page in Web development, through Activity. the setContentView () method displays the layout file on the Activity. Android parses the components in the XML file into visualized view components through the LayoutInflater class.

1) definition and use of layout files:

Resource location: res/layout/my_layout.xml (any name)

Layout XML file format:

<布局类 xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/stirng_name" (属性)>

<视图组件或者其他嵌套布局类>

Method for obtaining XML resources: Activity. setContentView ()

Resource reference method: in Java code: R. layout. my_layout

In the string XML file: @ [package:] layout/my_layout

Case: Layout file main. xml:

Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
Android: id = "@ + id/dimen_text"
Android: layout_width = "fill_parent"
Android: layout_height = "@ dimen/text_height"
Android: textColor = "@ color/white"
Android: background = "@ color/blue"
Android: text = "@ string/hello_world"/>

Java core code: setContentView (R. layout. main );

Source: http://blog.csdn.net/cl05300629/article/details/17716895 Author: Standing

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.