Five la s of Android

Source: Internet
Author: User

Android uses five layout objects: framelayout (Framework layout), linearlayout (linear layout), absolutelayout (absolute layout), and relativelayout (relative layout ), tablelayout (table layout ).
Framelayout:
Framelayout is the simplest layout object. It is customized as a blank standby area on your screen, and then you can fill in a single object-for example, an image you want to publish. All child elements are fixed in the upper left corner of the screen. You cannot specify a position for a child element in framelayout. The next child element directly overwrites the previous child element and blocks them in part or whole (unless the last child element is transparent ).
Let's take a look:

The main. XML Code is as follows:

JavaCode

    1. Android: layout_width = "fill_parent"
    2. Android: layout_height = "fill_parent"
    3. Android: text = "button"
    4. Android: layout_width = "fill_parent"
    5. Android: layout_height = "wrap_content"
    6. />
    7. Android: text = "textview"
    8. Android: textcolor = "# 0000ff"
    9. Android: layout_width = "wrap_content"
    10. Android: layout_height = "wrap_content"
    11. />

Linearlayout:
Linearlayout lists all child elements based on the vertical or horizontal attribute values you set for it. All child elements are stacked behind other elements, so each row in a vertical list only has one element, no matter how wide they are, A horizontal list only has one row height (the height is the height of the highest child element plus the border height ). Linearlayout maintains the interval and alignment between child elements (right, middle, or left ).
Linearlayout also supports specifying weight for individual child elements. The advantage is that it allows sub-elements to fill the remaining space on the screen. This also avoids a large screen where a bunch of small objects are crowded into a heap, but allows them to zoom in and fill the blank. When a child element specifies a weight value, the remaining space is allocated to the child elements according to the weight ratio specified by the child elements. The default weight value is 0. For example, if there are three text boxes, two of which specify the weight value as 1, the two text boxes will be scaled up proportionally and fill up the remaining space, the third text box is not enlarged.
Let's take a look:

The main. XM l code is as follows:

Java code

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent">
  6. <Linearlayout
  7. Android: Orientation = "vertical"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "fill_parent"
  10. Android: layout_weight = "2">
  11. <Textview
  12. Android: text = "welcome to Mr Wei's blog"
  13. Android: textsize = "15pt"
  14. Android: layout_width = "fill_parent"
  15. Android: layout_height = "wrap_content"
  16. />
  17. </Linearlayout>
  18. <Linearlayout
  19. Android: Orientation = "horizontal"
  20. Android: layout_width = "fill_parent"
  21. Android: layout_height = "fill_parent"
  22. Android: layout_weight = "1">
  23. <Textview
  24. Android: text = "red"
  25. Android: gravity = "center_horizontal" // The word is horizontally centered.
  26. Android: Background = "# aa0000"
  27. Android: layout_width = "wrap_content"
  28. Android: layout_height = "fill_parent"
  29. Android: layout_weight = "1"/>
  30. <Textview
  31. Android: text = "green"
  32. Android: gravity = "center_horizontal"
  33. Android: Background = "#00aa00"
  34. Android: layout_width = "wrap_content"
  35. Android: layout_height = "fill_parent"
  36. Android: layout_weight = "1"/>
  37. </Linearlayout>
  38. </Linearlayout>

Relativelayout:
Relativelayout allows child elements to specify their positions relative to other elements or parent elements (by ID ). Therefore, you can arrange two elements in the right-aligned, up/down, or in the center of the screen. Elements are arranged in order. Therefore, if the first element is in the center of the screen, other elements relative to the element are arranged in the relative position in the center of the screen. If you use XML to specify this layout, the associated elements must be defined before you define it.
Let's take a look:

The main. XML Code is as follows:

Java code

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent">
  5. <Textview
  6. Android: Id = "@ + ID/label"
  7. Android: layout_width = "fill_parent"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "welcome to Mr Wei's blog:"/>
  10. <Edittext
  11. Android: Id = "@ + ID/entry"
  12. Android: layout_width = "fill_parent"
  13. Android: layout_height = "wrap_content"
  14. Android: layout_below = "@ ID/label"/>
  15. <Button
  16. Android: Id = "@ + ID/OK"
  17. Android: layout_width = "wrap_content"
  18. Android: layout_height = "wrap_content"
  19. Android: layout_below = "@ ID/entry"
  20. Android: layout_alignparentright = "true"
  21. Android: layout_marginleft = "10dip"
  22. Android: text = "OK"/>
  23. <Button
  24. Android: layout_width = "wrap_content"
  25. Android: layout_height = "wrap_content"
  26. Android: layout_toleftof = "@ ID/OK"
  27. Android: layout_aligntop = "@ ID/OK"
  28. Android: text = "cancel"/>
  29. </Relativelayout>

Tablelayout:
Tablelayout distributes the positions of child elements to rows or columns. A tablelayout consists of many tablerow. Each tablerow defines a row (in fact, you can define other sub-objects, which will be explained below ). The tablelayout container does not display the border lines of row, cloumns, or cell. Each row has 0 or more cells, and each cell has one view object. A table consists of columns and rows in multiple cells. The table allows cells to be empty. Cells cannot span columns, which is different from HTML.
Let's take a look:

The main. XML Code is as follows:

Java code

  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" Android: layout_height = "fill_parent"
  4. Android: stretchcolumns = "1">
  5. <Tablerow>
  6. <Textview Android: layout_column = "1" Android: text = "open..."/>
  7. <Textview Android: text = "Ctrl-o" Android: gravity = "right"/>
  8. </Tablerow>
  9. <Tablerow>
  10. <Textview Android: layout_column = "1" Android: text = "Save..."/>
  11. <Textview Android: text = "Ctrl-s" Android: gravity = "right"/>
  12. </Tablerow>
  13. <View Android: layout_height = "2dip" Android: Background = "# ff909090"/> // The separator line in
  14. <Tablerow>
  15. <Textview Android: text = "X"/>
  16. <Textview Android: text = "Export..."/>
  17. <Textview Android: text = "Ctrl-e" Android: gravity = "right"/>
  18. </Tablerow>
  19. <View Android: layout_height = "2dip" Android: Background = "# ff909090"/>
  20. <Tablerow>
  21. <Textview Android: layout_column = "1" Android: text = "quit"
  22. Android: padding = "3dip"/>
  23. </Tablerow>
  24. </Tablelayout>

Absolutelayout:
Absolutelayout allows the child element to specify an accurate x/y coordinate value and display it on the screen. (0, 0) is the upper left corner. When you move down or to the right, the coordinate value increases. Absolutelayout has no page border and allows elements to overlap with each other (although not recommended ). We generally do not recommend absolutelayout unless you have a legitimate reason to use it, because it makes the interface code too rigid, so that it can work well on different devices.
Let's take a look:

The main. XM l code is as follows:

Java code

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Edittext
  8. Android: text = "welcome to Mr Wei's blog"
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content"
  11. />
  12. <Button
  13. Android: layout_x = "250px" // sets the X coordinate of the button.
  14. Android: layout_y = "40px" // sets the Y coordinate of the button.
  15. Android: layout_width = "70px" // you can specify the width of a button.
  16. Android: layout_height = "wrap_content"
  17. Android: text = "button"
  18. />
  19. </Absolutelayout>

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.