<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <person name = "xiaomo" age = "23" gender = "male"> </person> </resources>
Core code in java:
Resources r = getResources (); XmlResourceParser xrp = r. getXml (R. xml. person); try {// if the end of the file is not reached, continue loop while (xrp. getEventType ()! = XmlResourceParser. END_DOCUMENT) {// if it is the start tag if (xrp. getEventType () = XmlResourceParser. START_TAG) {String name = xrp. getName (); // determines whether the tag name is the same as that of personif (name. equals ("person") {counter ++; // obtain the tag attribute and append it to StringBuilder sb. append ("Number" + 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 the text myTextView of TextView. setText (sb. toString (); myButton. setText ("exit");} catch (XmlPullParserException e) {e. printStackTrace ();} catch (IOException e1) {e1.printStackTrace ();}}
MyImageView = (ImageView) findViewById (R. id. mypic); Resources r = getResources (); Drawable d = r. getDrawable (R. drawable. moto); myImageView. setImageDrawable (d );
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <TextView 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"/> </RelativeLayout>
SetContentView (R. layout. main );