In the last learning note, the parsing of XML files, actually some simple activity properties, some simple information, we can also put into the XML file, can be directly put into the res/vaules, by the system to parse, Without using Xmlpullparser to analyze yourself.
Dimension
Used for font size, spacing pading, and so on. Common size details see Android Learning Note (14): Activity-autocompletetextview, generally we use DIP/DP and SP, because and in (inch), MM, as well as PX (pixel), it and the physical screen size, Pixel density independent.
Add an XML file under Res/values and the name doesn't matter:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<dimen name= "thin" >10px</dimen>
<dimen name= "fat" >36sp</dimen>
</resources>
If we call in the layout file: <textview .... android:textsize= "@dimen/fat"/>; if we call in the Java source code: line1.settextsize (getresources (). Getdimension (r.dimen.thin));
Color
The same color can also be set in the resource file, the format of the color is #rgb, #ARGB, #RRGGBB, #AARRGGBB, using 16 binary. The same is the next XML file in Res/values, as follows:
<resources>
<color name= "Yellow_orange" >#FF5555</color>
<color name= "Forest_green" >#005500</color>
</resources>
If we call in the layout file: <textview ... android:textcolor= "@color/forest_green"/>; If we call in the Java source code: Line1.settextcolor (getresources (). GetColor (r.color.yellow_orange));
Array
There have been similar usages in listactivity, and it is also possible to set an array under Res/values XML. Here's how:
<resources>
<string-array name= "cities">
<item>philadelphia</Item>
<item>pittsburgh</Item>
<item>allentown/bethlehem</Item>
<item>erie</Item>
</String-array>
</resources>
In the Java source code, it can be obtained by string[] Cities = getresources (). Getstringarray (r.array.cities); A common way is to set the list item, such as Setlistadapter (new arrayadapter<string> (this,android). R.layout.simple_list_item_1,cities));
Handling of different situations
For applications, we need to set different processing for different objects, such as the international language version, the local language version, in addition, due to the variety of Android devices, to consider the direction of the screen (horizontal screen, vertical screen, support or do not support screen rotation), screen size and resolution, whether to support the touch screen, Whether there is a keyboard (digital or QWERTY), there are some other input devices (such as D-pad, is the arrow keys, in the feature phone is very common). Android processing these cases are through multiple resource directories, the directory name is specified, which indicates the adaptation of different situations.
Adaptation to different languages: placed in the Res/values/strings.xml as the default strings, specific languages, such as English, Spanish, respectively, corresponding to Res/values-en/strings.xml and res/values-es/ Strings.xml. The Android system will automatically adapt according to the language selection of the system, if there is no specific language, the default directory is used. XX in VALUES-XX is defined in ISO 639-1 and Chinese is "zh", which can be found in Wikipedia.
Language processing is relatively simple, but in the choice of equipment is more complex, because a lot of combinations. Android has rules for naming folders, such as screen orientation before touch screen, touch screen before screen size, such as Res/layout-large-port-notouch-qwerty, res/ Layout-large-port-stylus-nokeys,res/layout-normal-land-finger-qwerty and so on, the port here is portrait, and land (landscape) corresponds. But if we are going to deal primarily with each possible arrangement, it is almost impossible to look at some of the principles of Android's selection of files:
First, Android will not fit the file as a candidate, for example, there is a res/layout-large-port, and the current device is normal size, that is not suitable with-large, so this folder will be a candidate, if we can not find the corresponding file, will be here to choose. Remember when we deal with the image resources, we have seen a certain PNG image placed in the res/drawable-hdpi, and not placed in the res/drawable-mdpi, the normal size will be the first query-mdpi, if not, then query the other, So if you just place a picture resource, the folder you put it in can be called.
Second, the choice has the most matches, for example has Res/layout-large-port-finger-nokeys, and Res/layout-port, all matches, then the former matches the number is many, the former is preferred.
Third, if the match number is the same, for example Res/layout-large-finger-nokeys and Res/layout-large-port-nokeys, their match number is the same, this is the priority level before and after, Because the port is placed in front of the finger,-port has a higher priority level.
Based on these principles, we don't really need to combine all the possibilities, just give the relevant one.
RELATED Links: My Android development related articles
Android Learning Note (39): Resource resource (bottom)