"Alearning" chapter fourth Android layout component layout (i)

Source: Internet
Author: User

In this chapter we will study the Android layout components, in the previous chapter, we also initially use the LinearLayout layout, and then we will be more detailed learning and understanding of the layout files, and in the use of the case in-depth experience.

The Android interface is made up of layouts and components, and the layout is like a frame in the building, and the components are the bricks in the building. Components are arranged according to the layout requirements, which makes up the interface that the user sees. The five major layouts for Android are linearlayout (linear layout), Framelayout (single frame layout), relativelayout (relative layout), Absolutelayout (absolute layout) and tablelayout (table layout).

1. Linear layout (LinearLayout)
Linear layout of the form can be divided into two, the first horizontal linear layout of the second vertical linear layout, all of which are arranged in linear form one by one, the disadvantage of pure linear layout is very inconvenient to modify the display position of the control, so the development often in the form of linear layout and relative layout nested format layout.
2. Frame Layout (framelayout)
The principle is that any control that is drawn in a control can be overwritten by a control that is drawn later, and the last control that is drawn will cover the previous control.
3. Absolute layout (absolutelayout)
Using an absolute layout, you can set the X Y coordinate point of any control in the screen, and the control drawn after the frame layout will overwrite the control that was drawn before.
4. Relative layout (relativelayout)
The relative layout is the most powerful of the Android layout, first it can set the most properties, and secondly it can do the most things. Android phone screen resolution of a variety of so in order to consider the screen adaptive situation, so in the development of the proposed to use the relative layout of its coordinate range is relative, so use it to do the adaptive screen is correct.
5. Table layout (tablelayout)
You can set TableRow in a table layout to set the content and position of each row in the table, and to set the way in which the display indents and aligns.

"Blog column: http://blog.csdn.net/column/details/alearning.html"

linearlayout (linear layout)LinearLayout arranges the child elements in a vertical or horizontal order, each of which is behind the previous element. Not only can linearlayout layouts be nested, but other layouts can also be nested. The android:layout_weight of the child element attribute in LinearLayout is to describe the size of the child element in the remaining space, and the default value is 0.
This example expands the property configuration as follows:
    • Android:layout_weight weight
If there are two unequal text boxes (TextView, etc.) in LinearLayout, then their android:layout_weight values are 1 and 2 respectively, then the first text box will occupy two-thirds of the remaining space. The second text box will occupy One-third of the remaining space. Android:layout_weight follows the principle that the smaller the value, the higher the importance. This example demonstrates that the android:layout_weight is 1, so two linearlayout evenly distributes the width.
"Layout file" Activity_linearlayout.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:gravi ty= "center" android:orientation= "vertical" tools:context= ".        Mainactivity "> <!--single linearlayout-<linearlayout android:layout_width=" Match_parent "        android:layout_height= "Wrap_content" android:background= "#99CCFF" android:orientation= "Horizontal" > <textview android:layout_width= "match_parent" android:layout_height= "80DP" Android: gravity= "center" android:text= "#99CCFF"/> </LinearLayout> <!--the Lin with two horizontal average distributions under a single linearlayout Earlayout--<linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content "android:orientation=" Horizontal "> <linearlayout android:layout_width="Match_parent "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:back Ground= "#CCFF66" android:orientation= "vertical" > <textview Android:layout_widt H= "Match_parent" android:layout_height= "60DP" android:gravity= "center" Androi             d:text= "#CCFF66"/> </LinearLayout> <linearlayout android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" android:background= "#FF99 XX "android:orientation=" vertical "> <textview android:layout_width=" match_pare NT "android:layout_height=" 60DP "android:gravity=" center "android:text=" #FF99 "/> </LinearLayout> </LinearLayout> <!--The above two cases---<linearlayout and Roid:layout_width= "Match_parent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" Android:layout_w                eight= "1" android:background= "#CC99CC" android:orientation= "vertical" > <textview Android:layout_width= "Match_parent" android:layout_height= "120DP" Android:grav ity= "center" android:text= "#CC99CC"/> </LinearLayout> <linearlayout A            Ndroid:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" android:orientation= "vertical" > <linearlayout android:layout_width= "match_parent "Android:layout_height=" wrap_content "android:background=" #CC9933 "Android:o rientation= "Horizontal"> <textview android:layout_width= "match_parent" Android:layout_ height= "60DP" android:gravity= "center" android:text= "#CC9933"/> </ linearlayout> <linearlayout android:layout_width= "Match_parent" Android:lay                 out_height= "Wrap_content" android:background= "#99CCCC" android:orientation= "Horizontal" > <textview android:layout_width= "Match_parent" Android:layout_heig ht= "60DP" android:gravity= "center" android:text= "#99CCCC"/> </line Arlayout> </LinearLayout> </LinearLayout></LinearLayout>

Effect:

framelayout (frame layout)Framelayout is the simplest layout in the five layouts, in which the entire interface is treated as a blank fallback area where all the child elements cannot be placed, they are placed in the upper left corner of the area, and the subsequent child elements are directly above the preceding child elements. Masks the preceding child elements partially and completely.
This example expands the property configuration as follows:
    • Android:gravity the base position of the specified control
Note: This property has been described before, and this example is special, to illustrate that if you specify two parameters for this property, you can use the following format to "|" Connection.
Android:gravity= "Bottom|center"

"Layout file" Activity_framelayoutt.xml
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent ">    <textview android:layout_height= "Match_parent"        android:background= "#99CCFF"        android:layout_width= "Match_parent"        android:gravity= "Bottom|center"        android:text= "first layer \ \" #99CCFF ""/>         <textview android:layout_ height= "200DP"         android:background= "#CC99CC"        android:layout_width= "200DP"        android:gravity= "bottom |center "        android:text=" second level \ n "#CC99CC" "/>     <textview android:layout_height=" 100DP "        android: Layout_width= "100DP"        android:gravity= "Bottom|center"        android:background= "#99CCCC"        android:text= "Third tier \ \" #99CCCC ""/>     </FrameLayout>
Effect:
Absolutelayout (absolute layout) absolutelayout is an absolute position layout. The android:layout_x and Android:layout_y properties of child elements in this layout take effect and are used to describe the coordinate position of the child element. The upper-left corner of the screen is the coordinate origin (0,0), the first 0 represents the horizontal axis, the value is moved to the right, the second 0 represents the ordinate, and the value increases. Child elements in this layout can overlap each other. In the actual development, this layout is usually not used, because its interface code is too rigid, so that there is not a good adaptation of a variety of terminal equipment.
This example expands the property configuration as follows:
    • Android:layout_x the position of the x-axis of the specified control
    • Android:layout_y the position of the y-axis of the specified control
"Layout file" Activity_absolutelayout.xml.
<?xml version= "1.0" encoding= "Utf-8"? ><absolutelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <textview androi        D:layout_width= "60DP" android:layout_height= "60DP" android:layout_x= "30DP" android:layout_y= "30DP"        Android:background= "#009966" android:text= "#009966"/> <textview android:layout_width= "60DP" android:layout_height= "60DP" android:layout_x= "90DP" android:layout_y= "80DP" Android:background = "#CC6699" android:text= "#CC6699"/> <textview android:layout_width= "60DP" android:layout_he ight= "60DP" android:layout_x= "30DP" android:layout_y= "180DP" android:background= "#3366CC" Android oid:text= "#3366CC"/> <textview android:layout_width= "60DP" android:layout_height= "60DP" and roid:layout_x= "180DP" AndroidOid:layout_y= "90DP" android:background= "#FFCC99" android:text= "#FFCC99"/> <textview Android        : layout_width= "60DP" android:layout_height= "60DP" android:layout_x= "240DP" android:layout_y= "200DP" Android:background= "#FF6666" android:text= "#FF6666"/></absolutelayout>

Effect:

Resources

1, http://blog.chinaunix.net/uid-23544029-id-2985631.html

2, http://www.cnblogs.com/wisekingokok/archive/2011/08/23/2150452.html

3, http://blog.csdn.net/jzp12/article/details/7590591



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.