Go deep into the application of Five layout objects of Android

Source: Internet
Author: User

FrameLayout(Frame layout ),LinearLayout(Linear layout ),AbsoluteLayout(Absolute layout ),RelativeLayout(Relative layout ),TableLayout(Table layout)
FrameLayout:
FrameLayout is the simplest layout object. It is customized as a blank standby area on your screen. 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 overwrites the previous child element and blocks them in part or whole (unless the last child element is transparent ).


Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- We have added a Button here -->
<Button
Android: text = "button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<TextView
Android: text = "textview"
Android: textColor = "# 0000ff"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</FrameLayout>

The images on the right are usually on-demand videos. The two images are stacked together, and the last one overwrites the previous one. add links to the playback address;

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 spacing between child elements and alignment with each other (right, middle, or left ).

LinearLayout can also be specified for individual child elements.Weight. 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. Specify a sub-elementWeightValue, and the remaining space is specified according toWeightProportional allocation to these child elements. DefaultWeightThe value is 0. For example, if there are three text boxes, two of them specifyWeightIf the value is 1, the two text boxes are proportionally enlarged and the remaining space is filled. The third text box is not enlarged.

Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: orientation = "vertical">

<LinearLayout

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "vertical"

Android: layout_weight = "1">

<TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "hello, welcome to Livingstone's blog"

Android: textSize = "15pt"/> </LinearLayout>

<LinearLayout

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "horizontal"

Android: layout_weight = "2">

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "fill_parent"

Android: layout_weight = "2"

Android: background = "# aa0000"

Android: gravity = "center_horizontal"

Android: text = "red"/>

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "fill_parent"

Android: layout_weight = "1"

Android: background = "#00aa00"

Android: gravity = "center_horizontal"

Android: text = "green"/> </LinearLayout>

</LinearLayout>

AbsoluteLayout:

AbsoluteLayoutThe sub-element can specify the exact 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.AbsoluteLayoutThere is no page border, and elements can overlap with each other (although not recommended ). We generally do not recommendAbsoluteLayoutUnless 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.
RelativeLayout:
RelativeLayoutAllows child elements to specify their positions relative to other elements or parent elements (throughID). 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 useXMLTo specify thisLayoutBefore you define it, the associated elements must be defined.

Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: orientation = "vertical">

<LinearLayout

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "vertical"

Android: layout_weight = "1">

<TextView

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "hello, welcome to Livingstone's blog"

Android: textSize = "15pt"/> </LinearLayout>

<LinearLayout

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "horizontal"

Android: layout_weight = "2">

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "fill_parent"

Android: layout_weight = "2"

Android: background = "# aa0000"

Android: gravity = "center_horizontal"

Android: text = "red"/>

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "fill_parent"

Android: layout_weight = "1"

Android: background = "#00aa00"

Android: gravity = "center_horizontal"

Android: text = "green"/> </LinearLayout>

</LinearLayout>

TableLayout:

TableLayoutAssign the positions of child elements to rows or columns. OneTableLayoutBy manyTableRowComposition, eachTableRowDefineRow(In fact, you can define other sub-objects, which will be explained below ).TableLayoutContainer not displayedRow,CloumnsOrCell. EachRowWith 0 or moreCell; EachCellHaveViewObject. A table consists of columns and rows in multiple cells. The table allows cells to be empty. Cells cannot span columns.HTML.


Copy codeThe Code is as follows: <TableLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: stretchColumns = "1">
<TableRow>
<TextView android: layout_column = "1" android: text = "Open..."/>
<TextView android: text = "Ctrl-O" android: gravity = "right"/>
</TableRow>
<TableRow>
<TextView android: layout_column = "1" android: text = "Save..."/>
<TextView android: text = "Ctrl-S" android: gravity = "right"/>
</TableRow>
<View android: layout_height = "2dip" android: background = "# FF909090"/> // The separator line in
<TableRow>
<TextView android: text = "X"/>
<TextView android: text = "Export..."/>
<TextView android: text = "Ctrl-E" android: gravity = "right"/> // display TextView on the right
</TableRow>
<View android: layout_height = "2dip" android: background = "# FF909090"/>
<TableRow>
<TextView android: layout_column = "1" android: text = "Quit"
Android: padding = "3dip"/>
</TableRow>
</TableLayout>

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.