The Android XML file uses

Source: Internet
Author: User

First, layout files: In the layout directory, the use of more extensive;

We can define two or more sets of layouts for the application, for example: You can create a new directory Layout_land (on behalf of the phone landscape layout), Layout_port (on behalf of the phone vertical screen layout), the system will automatically find the most appropriate layout file according to different circumstances, But in the same interface two sets of different layout files should be the same file name, but placed in two different directories.

Second, the picture file: In the drawable directory, from the 2.1 version is divided into three directories,

    1. DRAWABLE-HDPI contains high-resolution images, such as WVGA (480x800), FWVGA (480x854)
    2. DRAWABLE-MDPI contains medium-resolution images such as HVGA (320x480)
    3. DRAWABLE-LDPI contains low-resolution images, such as QVGA (240x320)

The system will be based on the resolution of the machine to each of these folders to find the corresponding picture.

In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements.

We can put a good picture in this directory, or through a custom XML file to achieve the desired picture, for example, we can define shapge_1.xml into the drawable directory, the content is as follows:

<shape xmlns:android= "http://schemas.android.com/apk/res/android" android:shape= "Oval" > <!--android: Shape= "oval" means that the shape you want to draw is an ellipse, the default is rectangle, and the rectangle--><Gradient Android:startcolor= "#0055ff88"Android:centercolor= "#0055ff00"Android:centery= "0.75"Android:endcolor= "#00320077"Android:angle= "/>" <!--gradient produces a color gradient android:angle from which angle the integer multiples of only 90 can be--><solid android:color= "#ff4100ff"/> <!--solid means that the shape is solid, filled in, #ff4100ff为填充颜色--><Stroke Android:width= "2DP"Android:color= "#ee31ff5e"Android:dashwidth= "3DP"Android:dashgap= "2DP"/> <!--strokes are drawn in such a way that the width represents the thickness of the pen, Dashwidth represents the width of the dash, and Dashgap represents the distance between the small horizontal lines--><padding Android:left= "7DP"Android:top= "7DP"Android:right= "7DP"Android:bottom= "7DP"/> <!--and CSS padding should be a reason--><corners android:radius= "6dp"/> <!--corners represents a fillet with a radius of 5 pixels-- ></shape>

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

<?xml version= "1.0" encoding= "UTF-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item  android:state_pressed=" false "android:drawable=" @drawable/xxx1 "/>    <item  android:state_pressed= "true"  android:drawable= "@drawable/xxx2"/>    <item  android:state_ Focused= "true"  android:drawable= "@drawable/xxx3"/>    <--Here you can also add n more effects and actions as long as you use the--and    <item  android:drawable= "@drawable/xxx4"/></selector>

The above XML file can implement a control (assumed as a button), get focus, press the button, the effect of displaying different pictures in normal state, just need to define the control is to refer to the file name, for example:

<button     android:id= "@+id/button"     android:layout_width= "Wrap_content"      Android:layout_height= "Wrap_content"     android:background= "@drawable/button_add_x" ></ button><!--android:background= "@drawable/button_back" point to Button_back.xml file--

But what if our condition is not the type of event that the system already has, such as displaying different pictures according to the different values of ImageView based on a variable var? 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);} ...

Or you can use another convenient way to achieve the same function, under Res/drawable to create an XML file, the content is as follows

<level-list xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item android:maxlevel= "9" android:drawable= "@drawable/battery_0"/>    <item android:maxlevel= "android:drawable=" @drawable/ Battery_1 "/>    <item android:maxlevel=" "android:drawable=" @drawable/battery_2 "/>    <item Android:maxlevel= "android:drawable=" @drawable/battery_3 "/>    <item android:maxlevel=" Android: drawable= "@drawable/battery_4"/></level-list>

The ImageView src is then set to the created XML file in layout, and only the imageview.getdrawable () is used when transforming the picture in the program. SetLevel (50);
Android automatically selects the corresponding image based on the level value. The phone shows the remaining charge is the way to display different pictures.

Third, the menu file: In the menus directory, write code only in the Oncreateoptionsmenu method with Menuinflater loading in the OK. The format is as follows

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/enabled_item"Android:title= "Enabled"Android:icon= "@drawable/stat_happy"/> <item android:id= "@+id/disabled_item"Android:title= "Disabled"android:enabled= "false"Android:icon= "@drawable/stat_sad"/> <item android:id= "@+id/enabled_item_2"Android:title= "Enabled"Android:icon= "@drawable/stat_happy"/> <item android:id= "@+id/disabled_item_2"Android:title= "Disabled"android:enabled= "false"Android:icon= "@drawable/stat_sad"/></menu>

Four, resource file, in the values directory, the reason is called resource file, because the values directory XML files are resource as the root node,

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

<resources>    <string name= "Hello" >hello world!</string>    <string name= "App_name" > My Apps </string></resources>

2.colors.xml defines the color of the file, in the following format:

<resources>    <!--define picture color-    <drawable name= "Screen_background_black" > #ff000000 </ drawable>    <drawable name= "Translucent_background" > #e0000000 </drawable>    <drawable name= "Transparent_background" > #00000000 </drawable>    <!--define text color-    <color name= "solid_red" > #f00 </color>    <color name= "Solid_blue" > #0000ff </color>    <color name= "Solid_green" > #f0f0 </color>    <color name= "Solid_yellow" > #ffffff00 </color></resources>

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

<resources>    <string-array name= "Planets" >        <item>Mercury</item>        <item> venus</item>        <item>Earth</item>        <item>Mars</item>        <item> jupiter</item>        <item>Saturn</item>        <item>Uranus</item>        <item> neptune</item>        <item>Pluto</item>    </string-array>    <integer-array name= "Numbers" >        <item>100</item>        <item>500</item>        <item>800</ Item>     </integer-array></resources>

4.styles.xml defines the style of the file, divided into two purposes:

 style: Used as a unit in a single element (control) of the layout XML. For example: We can define a style style for TextView, which contains the font size and color of the text, and then uses it in TextView specific instances.  
 theme: Used in one unit for all activity in the application or for an activity in the application.   For example, we can define a theme that defines a set of colors for the foreground and background of the window frame and panel, and         to define the font size and color properties for the menu, which can be applied to the theme In all the activity in your program.  

 <resources> <!--Theme, which can be used to define the subject of activity--<style name= "theme.transparent" > <item nam E= "Android:windowistranslucent" >true  </item> <item name= "Android : Windowanimationstyle "> @android:style/animation.translucent</item> <item name=" Android: Windowbackground "> @drawable/transparent_background</item> <item name=" Android:windownotitle ">< Span style= "color: #0000ff;" >true  </item> <item name= "Android:colorforeground" > #fff </item> </style> <!-- Style, which can be used to define a view element, here is the style of ImageView-<style name= "imageview120dpi" > <item name= "android:src" >@ drawable/stylogo120dpi</item> <item name= "Android:layout_width" >wrap_content</item> <it EM name= "Android:layout_height" >wrap_content</item> </style></resources> 

Personally, in fact, whether it is theme or style, in fact, only the scope of application is different, the words should be based on the android:name= "XXXX" xxxx to distinguish it, it is obviously different.

5.dimen.xml defines the units of the file, the units of measurement in Android are as follows:

PX (pixels): screen actual pixels, often said resolution 1024*768pixels, is the transverse 1024px, portrait 768px, different devices display the same effect.

In (inch): The physical size of the screen, equal to 2.54 centimeters per inch.

MM (mm): Physical dimensions of the screen.

PT (Point): The physical size of the screen. 1/72 inches.

Dp/dip: pixels independent of density, a unit of abstraction based on screen density. 1DP = 1px on a monitor 160 dots per inch. However, the ratio of DP and PX varies with the screen density, and different devices have different display effects.

SP: A scale-independent pixel that is used primarily for font display of best for TEXTSIZE, as a unit of text-related size.

<resources>     <dimen name= "One_pixel" >1px</dimen>     <dimen name= "Double_density" >2DP </dimen>     

6.attrs.xml defines the properties of the file, mainly used in the custom components, the specific use of the method will be in the subsequent use of custom components in detail, the format is as follows:

<resources>         <declare-styleable name= "MyView" >             <attr name= "textcolor" format= "Color"/>             <attr name= "textSize" format= "Dimension"/>         </declare-styleable>     </resources>  

Five, animation files in the Anim directory, animation resources are divided into two kinds,

1. Realize the translate, scale, rotate, and alpha four variations of the image, and also set the playback characteristics of the animation, called the tween animation.

<set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate Android:interpolato R= "@android: Anim/accelerate_interpolator"Android:fromxdelta= "0" android:toxdelta= "android:fromydelta=" 0 "Android:toydelta= "android:duration="/> <scale android:interpolator= "@android: Anim/accelerate_interpolator"Android:fromxscale= "1.0" android:toxscale= "2.0" android:fromyscale= "1.0"Android:toyscale= "2.0" android:pivotx= "150%" android:pivoty= "150%"android:duration= "/>" <alpha android:fromalpha= "1.0" android:toalpha= "1.0"android:duration= "@android: Integer/config_mediumanimtime"/> <rotate .... Various properties ></rotate> <interpolator > can use its subclasses and attributes to define how the animation runs, fast, slow, slow, fast, etc. </Interpolator></set>

2. Frame animation, the resource that frames the settings, called Frame animation.

<animation-list xmlns:android= "http://android:oneshot="true">     <item android:drawable= "@drawable/rocket_thrust1″android:duration=" 200″/>     <item android:drawable= "@drawable /rocket_thrust2″android:duration= "200″/>     <item android:drawable=" @drawable/rocket_thrust3″android: duration= "200″/> </animation-list>

Six, the raw directory of files, is directly copied to the device in any file. They do not need to be compiled and added to the compressed file generated by your application compilation. Usually the audio or video files to be used for the application, etc.

To use these resources, you can call Resources.openrawresource (), which is the ID of the resource, which is r.raw. somefilename.

The file under the XML directory is a common XML file that needs to be used in the program. can be read at run time by calling Resources.getxml () .

The files in the assets directory are kept in the original file format, and the files need to be read in the form of a assetmanager byte stream.

1. First call Getassets () in the activity to get the Assetmanager reference.

2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and the access mode to get the input stream inputstream.

3. Then you read the file with the InputStream of the open file, and remember to Inputstream.close () after the read is done.

4. Call Assetmanager.close () to close Assetmanager.

Summary: In fact, the definition of so many XML configuration files in Android, in my opinion is to achieve the separation of the display layer and the data layer, improve maintainability, but also our program code becomes concise.

Reference:

Http://www.cnblogs.com/zhengtao/articles/1924940.html

The Android XML file uses

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.