Detailed explanation of the sample code used in Android XML files
1. Layout file: In the layout directory, it is widely used;
We can define two sets for the application Or multiple sets of layouts, for example: you can create new directories layout_land (representing the horizontal screen layout of mobile phones), layout_port (representing the vertical screen layout of mobile phones). The system will automatically find the most appropriate layout file according to different situations, but two different sets of layouts on the same interface The filenames of the files should be the same, just placed in two different directories.
2. Picture files: In the drawable directory, it is divided into three directories from version 2.1 onwards.
drawable-hdpi stores high-resolution pictures, such as WVGA (480×800) ,FWVGA (480×854)
drawable-mdpi stores medium-resolution pictures, such as HVGA (320×480)
drawable-ldpi stores low-resolution pictures, such as QVGA (240×320)
The system will go to these folders to find the corresponding pictures according to the resolution of the machine.
When developing a program, in order to be compatible with different platforms and different screens, it is recommended that different versions of images be stored in each folder according to needs.
We can put the prepared pictures in this directory, or realize the desired pictures through custom XML files. For example, we can define shape_1.xml and put it in the drawable directory, the content is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
When we want a control to display different pictures according to different states, we can control it directly in the program, or we can create an XML file in the drawable directory to achieve the same effect. For example: we can create a new file button_back in the drawable directory. .xml
1 2 3 4 5 6 7 8 |
|
The above XML file can implement a control (assumed to be a button), get the focus, press the button, and display the effects of different pictures under normal conditions. You only need to reference the file name when defining the control. , for example:
1 2 3 4 5 6 7 |
|
But what should we do when our condition is not an existing event type in the system, for example, ImageView displays different pictures based on the value of a variable var? You can write the following code in the program
1 2 3 4 5 6 7 8 |
|
Or you can use another simple method to achieve the same function. Create an xml file under res/drawable with the following content
1 2 3 4 5 6 7 |
|
and then in the layout Set the src of imageview to the created xml file. When changing the image in the program, you only need to use imageview.getDrawable().setLevel(50);
Android will automatically select the corresponding image according to the value of level. The mobile phone uses this method to display different pictures when displaying the remaining battery power.
3. Menu file: In the menu directory, when writing code, you only need to load it with MenuInflater in the onCreateOptionsMenu method. The format is as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
4. The resource file is in the values directory. It is called a resource file because the xml files in the values directory all have resource as the root node.
1 .strings.xml The file that defines strings, the format is as follows:
1 2 3 4 |
|
2.colors.xml The file that defines colors, the format is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
3.arrays.xml The file that defines arrays, The format is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
4.styles.xml The file that defines the style is divided into two purposes:
Style: used in the layout of a single XML element (control) in a unit manner. For example: we can define a style for TextView, including the font size and color of the text, and then use it on a specific instance of TextView.
Theme: used as a unit in all activities in the application or in a certain activity in the application. For example, we can define a Theme, which defines a set of colors for the foreground and background of the window frame and panel, and defines the textable size and color attributes for the menu. This Theme can be applied to all activities in your program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Personally, I think that whether it is Theme or Style, it is just the scope of application that is different. The distinction should be based on the xxxx of android:name="xxxx". It is obviously different.
5.dimen.xml File that defines units. There are the following measurement units in Android:
px (pixel): the actual pixels of the screen, the commonly used resolution is 1024* 768pixels means 1024px horizontally and 768px vertically. The display effect is the same on different devices.
in (inch): The physical size of the screen, each inch is equal to 2.54 centimeters.
mm(millimeters): The physical size of the screen.
pt (point): The physical size of the screen. 1/72 inch.
dp/dip: Density-independent pixel, an abstract unit based on screen density. On a 160 dots per inch monitor, 1dp = 1px. However, the ratio of dp to px will change with the change of screen density, and different devices have different display effects.
sp: pixels that have nothing to do with scale, mainly used for font display best for textsize, as a size unit related to text.
1 2 3 4 5 |
|
6.attrs.xml The file that defines attributes is mainly used in custom components. The specific usage will be introduced in detail in the subsequent How to use custom components. Its format is as follows:
1 2 3 4 5 6 |
|
五、动画文件 在anim目录下,动画资源分为两种,
1.实现图片的translate、scale、rotate、alpha四种变化,还可以设置动画的播放特性,称为Tween动画。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
2.帧动画,逐帧播放设置的资源,称为Frame动画。
1 2 3 4 5 6 |
|
六、raw目录下的文件,是直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。一般为应用要用到的音频或视频文件等等
要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。
七、xml目录下的文件,是程序中需要使用的普通xml文件。在运行时可以通过调用Resources.getXML()读取。
八、assets目录下的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
1. 先在Activity里面调用getAssets()来获取AssetManager引用。
2. 再用AssetManager的open(String fileName, int accessMode)方法则指定读取的文件以及访问模式就能得到输入流InputStream。
3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close()。
4.调用AssetManager.close()关闭AssetManager。
总结:其实android中定义如此多的XML配置文件,在我看来就是为了达到显示层和数据层的分离,提高了可维护性,也是我们的程序代码变得简洁。
The above is the detailed content of Detailed explanation of the sample code used in Android XML files. For more information, please follow other related articles on the PHP Chinese website!