Five major layouts for Android development

Source: Internet
Author: User

To accommodate a wide variety of interface styles, the Android system offers 5 layouts, 5 of which are:

LinearLayout (Linear layout)

Tablelayout (Table layout)

Relativelayout (relative layout)

Absolutelayout (Absolute layout)

Framelayout (Frame layout)

With these five layouts, the controls can be placed on the screen at will, and the size and position of the controls are adjusted accordingly as the screen size changes. Here are the relationships between the five layouts in the view's inheritance system:
<ignore_js_op>


One, LinearLayout (linear layout)

Aligns all child elements in one direction (vertical or horizontal)
A vertical list each row has only one child element (no matter how wide they are)
A horizontal list is just the height of a column (the height of the highest child element is populated)

The following is an example of a simple linear layout:
<ignore_js_op>

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <linearlayout
  3. Xmlns:android= "Http://schemas.android.com/apk/res/android"
  4. Android:layout_width= "Match_parent"
  5. android:layout_height= "match_parent" android:orientation= "vertical" >
  6. <edittext android:text= "EditText"
  7. 07
  8. Android:id= "@+id/edittext1"
  9. 09
  10. Ten android:layout_height= "Wrap_content"
  11. 11
  12. Android:layout_width= "Fill_parent" >
  13. 13
  14. </EditText>
  15. <linearlayout android:id= "@+id/linearlayout1"
  16. 16
  17. android:layout_height= "Fill_parent"
  18. 18
  19. Android:layout_width= "Fill_parent"
  20. 20
  21. Android:gravity= "Right" >
  22. <button android:id= "@+id/button2"
  23. 23
  24. android:text= "button"
  25. 25
  26. Android:layout_width= "Wrap_content"
  27. 27
  28. android:layout_height= "Wrap_content" ></Button>
  29. <button android:text= "button"
  30. 30
  31. Android:id= "@+id/button1"
  32. 32
  33. Android:layout_width= "Wrap_content"
  34. 34
  35. android:layout_height= "Wrap_content" ></Button>
  36. </LinearLayout>
  37. Panax Notoginseng </LinearLayout>
Copy Code

The outermost layout is a vertical linear layout with a width of the entire screen (fill_parent) and a height of exactly the child control (wrap_content). Then add a edittext, a horizontal layout of the linearlayout, in this linear layout, placed two button, the linear layout of the Gravity property is set to "right", so the two buttons inside the display.

Two, tablelayout (table layout)

Put the elements into rows and columns
Do not display rows, columns, or cell boundary lines
Cells cannot span rows, as in HTML
The tabular layout model manages child controls in the form of rows and columns, each of which behaves as a TableRow object, or, of course, a View object. TableRow you can add child controls, one for each column.

Android:layout_colum Official explanation: The index of the column in which this child should is, that is, to set the control in the columns in TableRow.
Android:layout_span Official explanation: Defines how many columns the child should span, that is, sets the number of columns that the control spans.

Android:collapsecolumns official explanation: The 0 based index of the columns to collapse. The column indices must is separated by a comma:1, 2, 5. That is, the columns specified in the tablelayout are hidden, and if more than one column needs to be hidden, use commas to separate the column numbers that need to be hidden.

Android:stretchcolumns official explanation: The 0 based index of the columns to stretch. The column indices must is separated by a comma:1, 2, 5. You can stretch all columns by using the value "*" instead. Note that a-column can be marked stretchable and shrinkable at the same time. This is also the setting of the specified column as extendable columns, which can stretch to fill all available space, and if more than one column needs to be set To stretch, separate the column numbers that need to be stretched with commas.

Android:shrinkcolumns official explanation: The 0 based index of the columns to shrink. The column indices must is separated by a comma:1, 2, 5. You can shrink all columns by using the value "*" instead. Sets the column that is specified to be shrunk. When a column that can be shrunk is too wide to make the other columns appear incomplete, the space is stretched vertically. When you need to set up multiple columns to shrink, separate the column numbers with commas.

Here is a simple example of how to use Tablelayout:
<ignore_js_op>

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  3. Android:layout_width= "Fill_parent"
  4. android:layout_height= "Fill_parent"
  5. android:stretchcolumns= "1" >
  6. <TableRow>
  7. <textview
  8. android:layout_column= "1"
  9. android:padding= "3dip" android:text= "Row1"/>
  10. Ten <textview
  11. android:text= "1"
  12. Android:gravity= "Right"
  13. android:padding= "3dip"/>
  14. </TableRow>
  15. <view
  16. android:layout_height= "2dip"
  17. android:background= "#FF909090"/>
  18. <TableRow>
  19. <textview
  20. android:text= "*"
  21. android:padding= "3dip"/>
  22. <textview
  23. android:text= "Row12"
  24. android:padding= "3dip"/>
  25. <textview
  26. android:text= "2"
  27. Android:gravity= "Right"
  28. android:padding= "3dip"/>
  29. </TableRow>
  30. <view
  31. android:layout_height= "2dip"
  32. android:background= "#FF909090"/>
  33. <TableRow>
  34. <textview
  35. android:layout_column= "1"
  36. android:text= "Row13"
  37. PNS android:padding= "3dip"/>
  38. </TableRow>
  39. </TableLayout>
Copy Code

Third, relativelayout (relative layout)

The relative layout of the child controls is relative layout based on the reference controls and parameters that they set. The reference control can be either a parent control or another child control, but the referenced control must be defined before the control that references it. The following is a simple example:
<ignore_js_op>

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <?xml version= "1.0" encoding= "Utf-8"?>
  3. <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  4. Android:layout_width= "Fill_parent"
  5. android:layout_height= "Fill_parent"
  6. >
  7. <analogclock
  8. Android:id= "@+id/aclock"
  9. Android:layout_width= "Wrap_content"
  10. Ten android:layout_height= "Wrap_content"
  11. Android:layout_centerinparent= "true"/>
  12. <digitalclock
  13. Android:id= "@+id/dclock"
  14. Android:layout_width= "Wrap_content"
  15. android:layout_height= "Wrap_content"
  16. android:layout_below= "@id/aclock"
  17. android:layout_alignleft= "@id/aclock"
  18. android:layout_marginleft= "40px"/>
  19. <textview
  20. Android:layout_width= "Wrap_content"
  21. android:layout_height= "Wrap_content"
  22. Android:text= "Current Time:"
  23. android:layout_toleftof= "@id/dclock"
  24. android:layout_aligntop= "@id/aclock"/>
  25. </RelativeLayout>
Copy Code

Iv. absolutelayout (Absolute layout)

A child control of an absolute layout needs to specify a horizontal ordinate value relative to the layout of this coordinate, otherwise it will be arranged in the upper-left corner as if it were a frame layout. Mobile apps need to adapt to different screen sizes, and this layout model cannot be adapted to the size of the screen, so the application is relatively small. The following is an example of an absolute layout:
<ignore_js_op>

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <absolutelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  3. Android:layout_width= "Fill_parent"
  4. android:layout_height= "Fill_parent"
  5. >
  6. <textview
  7. Android:layout_width= "Wrap_content"
  8. android:layout_height= "Wrap_content"
  9. android:layout_x= "10px"
  10. Ten android:layout_y= "10px" android:text= "Textview"/>
  11. <textview
  12. Android:layout_width= "Wrap_content"
  13. android:layout_height= "Wrap_content"
  14. android:layout_x= "30px"
  15. Android:layout_y= "30px" android:text= "Textview"/>
  16. <textview
  17. Android:layout_width= "Wrap_content"
  18. android:layout_height= "Wrap_content"
  19. android:layout_x= "50px"
  20. Android:layout_y= "50px" android:text= "Textview"/>
  21. </AbsoluteLayout>
Copy Code

V. Framelayout (frame layout)


The frame layout is the simplest form of layout. All views added to this layout are displayed in a cascading fashion. The first added control is placed at the bottom, and the last view added to the frame layout is displayed at the top level, and the previous control overrides the next layer of control. This display is somewhat similar to a stack. Here's a simple example

<ignore_js_op>

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  3. Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
  4. <linearlayout android:id= "@+id/linearlayout1"
  5. android:layout_height= "Match_parent"
  6. Android:layout_width= "Match_parent" >
  7. <button android:text= "button"
  8. Android:id= "@+id/button1"
  9. Android:layout_width= "Wrap_content"
  10. Ten android:layout_height= "Wrap_content" ></Button>
  11. </LinearLayout>
  12. <linearlayout android:layout_width= "Match_parent"
  13. Android:id= "@+id/linearlayout3"
  14. android:layout_height= "Match_parent"
  15. android:gravity= "Bottom|right" >
  16. <button android:text= "button"
  17. Android:id= "@+id/button3"
  18. Android:layout_width= "Wrap_content"
  19. android:layout_height= "Wrap_content" ></Button>
  20. </LinearLayout>
  21. <linearlayout android:layout_height= "Match_parent"
  22. Android:id= "@+id/linearlayout2"
  23. Android:layout_width= "Match_parent"
  24. Android:gravity= "Right" >
  25. <button android:text= "button"
  26. Android:id= "@+id/button2"
  27. Android:layout_width= "Wrap_content"
  28. android:layout_height= "Wrap_content" ></Button>
  29. </LinearLayout>
  30. <linearlayout android:layout_width= "Match_parent"
  31. Android:id= "@+id/linearlayout01"
  32. android:layout_height= "Match_parent"
  33. android:gravity= "Bottom|left" >
  34. <button android:id= "@+id/button01"
  35. android:text= "button"
  36. Android:layout_width= "Wrap_content"
  37. PNS android:layout_height= "Wrap_content" ></Button>
  38. </LinearLayout>
  39. </FrameLayout>
Copy Code

Here are some of the important attributes 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 cannot be found, then the parent element is the reference.

Second class: The attribute value must be the reference name of the id "@id/id-name"
Android:layout_below at the bottom of an element
Android:layout_above at 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 aligns the top edge of this element with the top edge of an element
Android:layout_alignleft aligns the left edge of this element with the left edge of an element
Android:layout_alignbottom aligns the bottom edge of this element with the bottom edge of an element
Android:layout_alignright aligns the right edge of this element with the right edge of an element

Class III: Attribute values are specific pixel values, such as 30dip,40px
Android:layout_marginbottom distance from the bottom edge of an element
Android:layout_marginleft distance from the left edge of an element
Android:layout_marginright distance from the right edge of an element
Android:layout_margintop distance from the top edge of an element

Excerpt from: http://bbs.chinaunix.net/thread-3654213-1-1.html

Five major layouts for Android development

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.