Android programming Absolute Layout absolutelayout and Relative layout Relativelayout instance detailed _android

Source: Internet
Author: User

This paper analyzes the absolute layout absolutelayout and relative layout relativelayout of Android programming. Share to everyone for your reference, specific as follows:

One, absolute layout absolutelayout

Absolute positioning Absolutelayout, but also called coordinate layout, you can directly specify the absolute position of the child elements, this layout is simple and direct, intuitive strong, but because the screen size of the mobile phone is relatively large, the use of absolute positioning will be relatively poor adaptability.

Let's take an example to see: In the example of the robot picture size is 250x250, you can see we use android:layout_x and android:layout_y to specify the vertical and horizontal coordinates of the child elements.

Xml/html Code:

<?xml version= "1.0" encoding= "Utf-8"?> <absolutelayout android:id= "@+id/absolutelayout01" Android:layout_ Width= "Fill_parent" android:layout_height= "fill_parent" xmlns:android= "http://schemas.android.com/apk/res/" 
Android "android:background=" #fff "><imageview android:src=" "@drawable/android" android:layout_y= "40dip" Android:layout_width= "Wrap_content" android:layout_x= "35dip" android:id= "@+id/imageview01" android:layout_height= "Wrap_content" > </ImageView> <textview android:layout_height= "wrap_content" android:layout_width= "fill _parent "android:id=" @+id/textview01 "android:text=" Android2.2 Learning Guide "android:textcolor=" #0f0 "android:textsize=" 28dip "android:layout_y=" 330dip "android:layout_x=" 35dip "> </TextView> <textview android:layout_height= 
"Wrap_content" "android:layout_width=" Fill_parent "android:id=" @+id/textview02 "android:text=" illustrated, the theory is clear, the operation of Strong " 
Android:textcolor= "#333" android:textsize= "18dip" android:layout_y= "365dip"android:layout_x= "35dip" > </TextView> </AbsoluteLayout>

 

Let's look at the display under the WQVGA simulator:

Then look at the display under the WVGA800 simulator:

Tip: In absolute positioning, if the child elements do not set layout_x and layout_y, their default value is 0, which means that it will appear in the upper-left corner of the same element as in Framelayout.

Second, relative layout relativelayout

Relative layout relativelayout allows child elements to specify their location relative to their parent element or sibling element, which is one of the most common layout methods in the actual layout. It has a lot of flexibility, of course, many of the attributes, the operation is difficult, the possibility of conflict between the attributes, the use of relative layout to do more testing.

Here we use the relative layout to do the above example, first place a picture, the other two text relative to the previous element positioning:

Xml/html Code:

<?xml version= "1.0" encoding= "Utf-8" ><relativelayout "android:id=" @+id/relativelayout01 Width= "Fill_parent" android:layout_height= "fill_parent" android:background= "#fff" xmlns:android= "http:// Schemas.android.com/apk/res/android "><imageview android:id=" @+id/imageview01 "android:src=" @drawable 
Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:layout_margintop=" 40dip " 
> </ImageView> <textview android:layout_height= "wrap_content" android:layout_width= "Wrap_content" 
Android:id= "@+id/textview01" android:text= "Android2.2 Learning Guide" android:textcolor= "#0f0" android:textsize= "28dip" android:layout_below= "@id/imageview01" android:layout_centerhorizontal= "true" android:layout_margintop= "10dip" 
> </TextView> <textview android:layout_height= "wrap_content" android:layout_width= "Wrap_content" Android:id= "@+id/textview02" android:text= "illustrated, the theory is clear, the operability is strong" android:textcolor= "#333" Android:teXtsize= "18dip" android:layout_below= "@id/textview01" android:layout_centerhorizontal= "true" Android:layout_ 

 margintop= "5dip" > </TextView> </RelativeLayout>

Let's look at the display under the WQVGA simulator:

Look at the display on the larger screen (WVGA800) emulator:

From the image above, you can see that the interface effect is basically consistent, rather than holed up in the upper left corner like absolute positioning; students see the function of automatic scaling is to use the dip to do the benefits of unit.

Here are some of the important properties that Relativelayout uses:

First Class: property value is True or False
Android:layout_centerhrizontal Horizontal Center
Android:layout_centervertical Vertical Center
Android:layout_centerinparent is completely centered relative to the parent element
Android:layout_alignparentbottom snaps to the bottom edge of the parent element
Android:layout_alignparentleft snaps to the left edge of the parent element
Android:layout_alignparentright snaps to the right edge of the parent element
Android:layout_alignparenttop snaps to the top edge of the parent element
android:layout_alignwithparentifmissing if the corresponding sibling element is not found, use the parent element as a reference
Class Two: attribute value must be the reference name of ID "@id/id-name"
The android:layout_below is below an element
Android:layout_above on the top of an element
Android:layout_toleftof on the left side of an element
Android:layout_torightof on the right side of an element
Android:layout_aligntop the top edge of this element and the top edge of an element
Aligns the left edge of this element with the left edge of an element android:layout_alignleft
Android:layout_alignbottom the bottom edge of this element and the bottom edge of an element
Aligns the right edge of this element with the right edge of an element android:layout_alignright
Class III: Attribute values are specific pixel values, such as 30dip,40px
Android:layout_marginbottom the distance from the bottom edge of an element
Android:layout_marginleft the distance from the left edge of an element
Android:layout_marginright the distance from the right edge of an element
Android:layout_margintop the distance from the top edge of an element

Let's do the above example again, this time put some more attributes inside, we test:

Xml/html Code:

<?xml version= "1.0" encoding= "Utf-8" ><relativelayout "android:id=" @+id/relativelayout01 Width= "Fill_parent" android:layout_height= "fill_parent" android:background= "#cfff" the setting of the color is ARGB, the first C is transparency xmlns: Android= "Http://schemas.android.com/apk/res/android" > <imageview android:id= "@+id/imageview01" android:src= "@drawable/android" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ margintop= "40dip" android:layout_centerhorizontal= "true" > </imageview><textview android:layout_height 
= "Wrap_content" android:layout_width= "wrap_content" android:id= "@+id/textview01" android:text= "Android2.2 Learning Guide" Android:textcolor= "#0f0" android:textsize= "28dip" android:layout_below= "@id/imageview01" Android:layout_ Centerhorizontal= "true" android:layout_margintop= "10dip" > </textview><textview android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:id= "@+id/textview02" Android:teXt= "Illustrated, clear theory, strong operability" android:textcolor= "#333" android:textsize= "18dip" android:layout_below= "@id/textview01" Android:layout_centerhorizontal= "true" android:layout_margintop= "5dip" > </TextView> <textview android: layout_height= "Wrap_content" android:layout_width= "wrap_content" android:id= "@+id/textview03" android:text= " AlignTop "android:textcolor=" #333 "android:textsize=" 18dip "android:layout_aligntop=" @id/imageview01 " Aligns with the top edge of the ImageView01 android:layout_centerhorizontal= "true" > </textview><textview android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:id= "@+id/textview04" android:text= "AlignLeft" Android: Textcolor= "#333" android:textsize= "18dip" android:layout_alignleft= "@id/imageview01" Android:layout_ Centerhorizontal= "true" > </textview><textview android:layout_height= "wrap_content" Android:layout_ Width= "Wrap_content" android:id= "@+id/textview05" android:text= "AlignRight" android:textcolor= "#333" aNdroid:textsize= "18dip" android:layout_alignright= "@id/imageview01" android:layout_centerhorizontal= "true" > </textview><textview android:layout_height= "wrap_content" android:layout_width= "Wrap_content" Android:id = "@+id/textview06" android:text= "AlignBottom" android:textcolor= "#333" android:textsize= "18dip" Android:layout_ alignbottom= "@id/imageview01" android:layout_centerhorizontal= "true" > </TextView> </relativelayout 

 >

Absolute layout Absolutelayout and relative layout relativelayout content is finished, I hope this article will help you with Android program design.

Related Article

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.