Detailed explanation of the sample code used by the AndroidXML file

Source: Internet
Author: User
We can define two or more layouts for an application. for example, you can create a directory named layout_land (indicating the horizontal screen layout of the mobile phone) and layout_port (indicating the portrait layout of the mobile phone ), the system will automatically find the most suitable layout file according to different situations, but the file names of the two sets of different layout files on the same interface should be the same, but they are placed in two different directories. Detailed explanation of the sample code used by the Android XML file

I. layout files: the layout directory is widely used;

We can define two or more layouts for an application. for example, you can create a directory named layout_land (indicating the horizontal screen layout of the mobile phone) and layout_port (indicating the portrait layout of the mobile phone ), the system will automatically find the most suitable layout file according to different situations, but the file names of the two sets of different layout files on the same interface should be the same, but they are placed in two different directories.

II. image files: In the drawable Directory, there are three directories after version 2.1,

Drawable-hdpi stores high-resolution images, such as WVGA (480x800) and FWVGA (480x854)

Drawable-mdpi stores medium-resolution images, such as HVGA (320x480)

Drawable-ldpi stores low-resolution images, such as QVGA (240x320)

The system will find the corresponding images in these folders according to the machine resolution.

To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.

We can put the prepared images in this directory or use a custom XML file to implement the desired images. for example, we can define shapge_1.xml and put it in the drawable Directory. the content is as follows:

  
  
   
  
   
  
   
  
   
  
   
  
 

If you want a control to display different images in different states, you can directly control the control in the program or create an XML file in the drawable directory to achieve the same effect. for example: we can create a new file named button_back.xml in the drawable directory.

 
     
      
      
  <-- N more effects and actions can be added here as long as you use -->
  
 

The preceding XML file can implement a control (assumed as a button), get the focus, and press the button to display the effects of different images under normal conditions. you only need to reference the file name in the definition control, for example:

 

However, what should we do if our condition is not an existing event type of the system, for example, to display different images based on the var value of the ImageView variable? You can write the following code in the program:

If (condition 1) {image. setBackground (R. id. xxx1);} else if (condition 2) {image. setBackground (R. id. xxx2 );}...

Alternatively, you can use another simple method to implement the same function. create an xml file under res/drawable with the following content:

     
      
      
      
      
  
 

Then, in layout, set the src of imageview to a created xml file. when converting an image in the program, you only need to use imageview. getDrawable (). setLevel (50 );
Android automatically selects the corresponding image based on the level value. This method is used to display different pictures of the remaining power displayed on the mobile phone.

III. menu File: in the menu Directory, when writing code, you only need to load it in the onCreateOptionsMenu method with MenuInflater. The format is as follows,

     
      
      
      
  
 

4. the resource file is called the resource file under the values directory because the xml file in the values Directory uses resource as the root node,

1. strings. xml defines the string file in the following format:

     
  
   
Hello World!
      
  
   
My Applications
  
 

2. colors. xml:

     
      
  
   
# Ff000000
      
  
   
# E0000000
      
  
   
#00000000
      
      
  
   
# F00
      
  
   
# 0000ff
      
  
   
# F0f0
      
  
   
# Ffffff00
  
 

3. arrays. xml defines the array file in the following format:

     
          
   
    Mercury
           
   
    Venus
           
   
    Earth
           
   
    Mars
           
   
    Jupiter
           
   
    Saturn
           
   
    Uranus
           
   
    Neptune
           
   
    Pluto
       
      
          
   
    100
           
   
    500
           
   
    800
        
  
 

4. styles. xml defines styles in two ways:

Style: used in a single element (control) of XML layout in one unit. For example, we can define a style for TextView, including the font size and color of the text, and then use it in a specific example of TextView.
Theme: used in one unit in all the activities of the application or in a specific Activity of 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 size and color attributes of the text for the menu, this Theme can be applied to all the activities in your program.

    
     
      
      
  
 

In my opinion, whether Theme or Style is actually different in the scope of the application. The difference should be based on the android: name = "xxxx" xxxx, obviously different.

5. dimen. xml defines the unit File. the measurement units in android include the following:

Px (pixel): The actual pixel of the screen. The resolution is usually 1024*768 pixels, that is, 1024px in the horizontal direction and 768px in the vertical direction. different devices have the same display effect.

In (inches): the physical size of the screen. each inch equals 2.54 cm.

Mm (mm): physical size of the screen.

Pt (point): physical size of the screen. 1/72 inch.

Dp/dip: density-independent pixel, an abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch. However, the ratio of dp to px varies with the screen density, and different devices have different display effects.

Sp: Pixel irrelevant to the scale. it is mainly used to display the best for textsize in the font, and is used as the unit of size related to the text.

      
  
   1px
       
  
   2dp
       
  
   16sp
   
 

6. the attrs. xml file defines attributes and is mainly used in custom components. the specific usage will be detailed in how to use custom components. the format is as follows:

          
                                     
       
 

5. There are two types of animation resources in the anim Directory,

1. implement four changes of the image: translate, scale, rotate, and alpha. you can also set the playing characteristics of the animation, which is called the Tween animation.

                    
      
          
      
  
   
You can use its subclass and attribute to define the animation running mode, such as fast first, slow, and fast.
  
 

2. Frame animation: a Frame animation.

http://schemas.android.com/apk/res/android” android:oneshot=”true”>     
      
      
  

6. files in the raw directory are directly copied to any files on the device. They do not need to be compiled and are added to the compressed files generated by your application compilation. Generally, it is the audio or video files used by applications.

To use these Resources, you can call Resources. openRawResource (). the parameter is the resource ID, that is, R. raw. somefilename.

7. files in the xml directory are common xml files used in the program. You can call Resources. getXML () to read data at runtime.

8. files in the assets directory are kept in the original file format. you need to use AssetManager to read files in the form of byte streams.

1. call getAssets () in the Activity to obtain the AssetManager reference.

2. use AssetManager's open (String fileName, int accessMode) method to specify the file to be read and the access mode to obtain the input stream InputStream.

3. use inputStream of the open file to read the file. after reading the file, remember inputStream. close ().

4. call AssetManager. close () to close AssetManager.


Summary: In fact, android defines so many XML configuration files, in my opinion, to achieve the separation of the display layer and the data layer, improve the maintainability, and make our program code concise.

The above is a detailed description of the sample code used by the Android XML file. For more information, see other related articles in the first PHP community!

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.