Resource is the static information outside the java source code. For example, layout. Resource is stored in the res/directory as a file. In addition to res/raw/, Android will automatically parse the file, for example, layout. We do not need to parse XML Encapsulation by ourselves. We have used res/layout/and res/anim/before.
Image: The file is stored in res/drawable/, and the icon of the application is stored.
Raw: res/raw/, which is a file of any format required by the program. However, the system does not parse the file, which is not required by Adroid Framework.
String, color, array, and dimension: IN res/values, these constants are separated from our java source code to facilitate program internationalization or localization.
XML, located in res/xml/. These static XML files are used to save program data and structure.
String
Define resource
Defining in resource is conducive to internationalization or localization. Even if you do not need to translate it into other languages, you can easily detect or modify it without looking for it everywhere in the program, therefore, this is a common processing method. Android also supports common extension methods. Different formats can be set through style. String is defined in res/values/strings. xml. The root node is resources, for example:
<resources><string name="quick">The quick brown fox...</string><string name="laughs">He who laughs last...</string<string name="c20_my_name">My name is %1$s %2$s</string><string name="c20_rich_text">My name is <b>%1$s</b> <i>%2$s</i></string></resources>
If the string contains "and", you need to use \ "or \ '. If the string contains only one single quotation mark, you can use" xxxxx's xxxxxx "to enclose it in double quotation marks.
In the layout file, use @ string/element_name, for example, @ string/quick to reference content. In java source code, use R. string. element_name is used as the ID and is obtained using getString (), for example, getString (R. string. laughs ).
String format is supported.
For example, in strngs. defined in xml: <string name = "c20_my_name"> My name is % 1 $ s % 2 $ s </string>. The placeholder symbol can be replaced by a variable, for example
String strFormat = getString(R.string.c20_my_name);String strResult = String.format(strFormat,"Tim","Martin");
The strResult is: My name is Tim Martin.
Rich Text
If we need rich string, we can also define the format through HTML. We have learned how to display it through webkit. However, you can also display the Html format in TextView.
Example: <string name = "c20_rich_text"> My name is & lt; B & gt; % 1 $ s & lt;/B & gt; & lt; i> % 2 $ s & lt;/I & gt; </string> in the program, we display the content through textview and a textview in html format, the last one is displayed through webkit:
TextView lable = (TextView) findViewById (R. id. c20_lable); TextView lable2 = (TextView) findViewById (R. id. c20_lable2); WebView web = (WebView) findViewById (R. id. c20_web); String strFormat = getString (R. string. c20_rich_text); String strResult = String. format (strFormat, "Tim", "Martin"); String strResult2 = String.format(strFormat,TextUtils.html Encode ("Tim"), TextUtils.html Encode ("Martin");/* above, the results of strResult and strResult2 are the same. My name is <B> Time <B> <I> Martin </I> * the key is the following setText (Html. fromHtml (strResult) to convert a simple HTML to a formatted text object */lable. setText (strResult); lable2.setText (Html. fromHtml (strResult2); web. loadData (strResult, "text/html", "UTF-8 ");
2.11.2 supplement: insiku: TextUtils.html Encode is used to prevent the input string from containing some reserved characters in HTML, such as <>
&, These characters have special meanings in HTML, and must be converted into character entities
Picture
The image file can be PNG, JPEG, or GIF. We recommend that you use PNG instead of GIF. The file is placed under res/drawable, android has three folders: drawable-hdpi/drawable-mdpi/and drawable-ldpi, which correspond to different sizes. In fact, there is no big difference. Assume that the file name is foo.png, And the name is @ drawable/foo in the resource XML file. In Java source code, the resource ID is R. drawable. foo.
Example: Put a png file app_icon into drawable. I put it into drawable-hdpi/. In the Adroid XML file, the following is the running result. The book icon in the middle is our ImageButton, you only need to specify the image location through android: src.
......<ImageButton android:id="@+id/c20_format2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/app_icon"/>......
Xml file
Xml files can be used as configuration files to store our data. The xml file can be located in the res/xml directory. For example, we can place an xml file "words. xml" in this directory, as shown below:
<?xml version="1.0" encoding="utf-8"?><ListItems desrciption="Put the entry as the item in Android List"><Entry value="One" /><Entry value="Two" /><Entry value="Three" /><Entry value="Four" /><Entry value="Five" /><Entry value="Six" /><entry>Hello</entry></ListItems>
XmlPullParser can be used to interpret xml files. There are two common xml formats: <entry> Hello </entry> and <Entry value = "Six"/>. The latter is used in Android XML, this method is also very convenient in XmlPullParser. Its usage is summarized as follows:
- XmlPullParser checks down through next ()
- XmlPullParser is event-driven. If END_DOCUMENT is detected, stop the detection.
- XmlPullParser is event-driven. When START_TAG is detected, you can read the content of <AAAA bbbb = xxxx c = yyyyy>, where AAAA can be obtained through getName (). The number of attributes, you can use getAttributeCount () to obtain the attributes. You can use getAttributeName (index) and getAttributeValue (index) to obtain the nouns and content of the attributes.
- XmlPullParser is event-driven. If it is an END_TAG, It is </AAAA>. You can use getName () to obtain AAAA content.
- For the <entry> Hello </entry> method, if you want to obtain the intermediate value, the event is XmlPullParser. TEXT. You can get the content through getText.
- The exception capture method should be used to handle xml files that may not be written correctly or are incorrectly handled during the interpretation process.
In the following example, we analyze and process the words. xml and use the Entry value as the content of each item in listActivity.
/* 1. xmlPullParser uses next () to check down in sequence * 2. xmlPullParser is event-driven. If END_DOCUMENT is detected, stop the detection. * 3. xmlPullParser is event-driven. When START_TAG is detected, you can read the content of <AAAA bbbb = xxxx c = yyyyy>, where AAAA can be obtained through getName (). The number of attributes, you can use getAttributeCount () to obtain the attributes. You can use getAttributeName (index) and getAttributeValue (index) to obtain the nouns and content of the attributes. * 4. xmlPullParser is event-driven. If it is an END_TAG, It is </AAAA>. You can use getName () to obtain AAAA content * 5. for the <entry> Hello </entry> method, if you want to obtain the intermediate value, the event is XmlPullParser. TEXT, which can be obtained through getText. * 6. because the xml file may be incorrectly written, or the xml file cannot be correctly written during the interpretation process, exception capture should be used to handle */public class Chapter20Test3 extends ListActivity {private ArrayList <String> items = new ArrayList <String> (); protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);/* Step 1: parse the words in a specific xml file. At, use exception capture to prevent program errors. */try {/* Step 2: get the xml file and give the XmlPullParser object */XmlPullParser xpp = getResources (). getXml (R. xml. words);/* Step 3: Use a loop, Parse the XML gradually until the end of the xml file, corresponding to and */while (xpp. getEventType ()! = XmlPullParser. END_DOCUMENT) {/* Step 4: Obtain the resolution of the target ListItems, and use method: ListItems for processing, corresponding to 3rd points */if (xpp. getEventType () = XmlPullParser. START_TAG) {if (xpp. getName (). equals ("ListItems") {getItems (xpp) ;}} xpp. next () ;}} catch (Throwable t) {Toast. makeText (this, "Failed:" + t. toString (), 2000 ). show ();} setListAdapter (new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, items);}/* parse <ListItems ......> .... </Listitem> to obtain the value of each entry. handle exceptions. */private void getItems (XmlPullParser xpp) throws Throwable {while (true) {xpp. next ();/* <ListItems>... </ListItems> the content has been searched, or the file has been cleared. */if (xpp. getEventType () = XmlPullParser. END_TAG & xpp. getName (). equals ("ListItems") | xpp. getEventType () = XmlPullParser. END_DOCUMENT) break; if (xpp. getEventType () = XmlPullParser. START_TAG) {/* observation point: Process 3rd points, read the attribute name and value */if (xpp. getName (). equals ("Entry") {for (int I = 0; I <xpp. getAttributeCount (); I ++) {if (xpp. getAttributeName (I ). equals ("value") {items. add (xpp. getAttributeValue (I) ;}}/ * observation point: Process 5th points and Process <name> value </name> */if (xpp. getName (). equals ("entry") {xpp. next (); if (xpp. getEventType () = XmlPullParser. TEXT) items. add (xpp. getText () ;}}}@ Overrideprotected void onListItemClick (ListView l, View v, int position, long id) {super. onListItemClick (l, v, position, id); Toast. makeText (this, items. get (position), Toast. LENGTH_LONG ). show ();}}
Related links:
My Android development articles