Take a look at the six major Android layouts

Source: Internet
Author: User

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), Relativelayout (relative layout), Framelayout (single frame layout), Absolutelayout (absolute layout) and tablelayout (table layout) also have a new grid layout (GridLayout) after Android4.0.

First of all:

1, linearlayout layout:

LinearLayout arranges the child elements in a vertical or horizontal order, each of which is behind the previous element. If it is arranged vertically, it will be a single row of n rows of structure, each row will have only one element, regardless of the width of the element, if it is arranged horizontally, then it will be a single row n column structure.

If two rows of two columns are constructed, the usual way is to arrange the two elements vertically, and each element contains a linearlayout to be arranged horizontally.

The child element attribute Android:layout_weight in LinearLayout takes effect, which is used to describe the size ratio of the child element in the remaining space.

Add a row with only one text box, then its default value is 0,android:layout_weight= "0" (default)

If there are two equal-length text boxes in a row, their android:layout_weight value can be the same as 1.

If there are two unequal text boxes in a row, their android:layout_weight values are 1 and 2, respectively, then the first text box will occupy two-thirds of the remaining space, and 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.

Some property values for linear layouts:

android:gravity--sets the alignment of individual components in the layout manager. This property supports properties such as top, bottom, left, right, center_vertical, fill__vertical, Center_horizontal, Fill__horizontal, and so on.

android:orientation--sets how the layout manager internal components are arranged. One of two values can be set to horizontal (horizontally arranged), Vertical (vertically arranged).

2, Relativelayout layout:

The relativelayout completes the layout according to the position relationship between the child elements. Position-related attributes in the child elements in this layout will take effect. such as Android:layout_below, Android:layout_above and so on. Child elements Specify a positional relationship through these attributes and their respective ID mates. Note When you specify a location relationship, the ID of the reference must be defined before the reference, or an exception will occur.

The location attributes commonly used in Relativelayout are as follows:

android:gravity--to set the alignment of individual components in the layout manager

android:layout_toleftof--the component is on the left side of the reference component

android:layout_above--the component is above the referencing component

android:layout_below--the component is below the reference component

android:layout_alignparentleft--whether the component aligns to the left side of the parent component

android:layout_alignparentright--whether the component is aligned to the right end of its parent component

android:layout_alignparenttop--whether the component aligns to the top of the parent component

android:layout_alignparentbottom--whether the component aligns to the bottom of the parent component

android:layout_centerinparent--whether the component is centered relative to the parent component

android:layout_centerhorizontal--whether the component is centered horizontally

android:layout_centervertical--whether the component is vertically centered

Relativelayout is one of the most flexible layout structures in the Android five layout structure, and is more suitable for some complex interface layouts. The following example shows a case where the first text box is aligned with the bottom of the parent component, the second text box is above the first text box, and the third text box is to the left of the second text box.

3,framelayout layout:

Framelayout is the simplest layout of the five layouts in which the entire interface is treated as a blank spare area where all the child elements cannot be placed, and they are placed in the upper left corner of the area. And the subsequent child elements are directly above the preceding child elements, partially and completely obscured by the preceding child elements. The display works as follows, the first TextView is completely obscured by the second TextView, and the third TextView obscures part of the second TextView.

4,absolutelayout 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. These need to be controlled by developers themselves. Child elements in this layout can overlap each other. In real-world development, this layout is usually not used because its interface code is too rigid to be able to fit a variety of terminals well.

5,tablelayout layout:

Tablelayout as the name implies, this layout is a table layout and applies to the layout format of n row n columns. A tablelayout is made up of many TableRow, and a tablerow represents a row in tablelayout.

TableRow is a subclass of LinearLayout, its Android:orientation property value is horizontal, and its android:layout_width and Android:layout_ The Height property value is constant match_parent and wrap_content. So its sub-elements are all horizontally aligned, and the width is high and consistent. This design allows each TableRow to be the same as the cells in the table. In TableRow, cells can be empty, but cannot span columns.

6,gridlayout layout:

GridLayout, like the LinearLayout layout, is divided horizontally and vertically, by default a horizontal layout, and a control is arranged from left to right next to a control, but after you specify android:columncount to set the properties of the number of columns, The control wraps itself in a line-wrap arrangement. On the other hand, for child controls in a GridLayout layout, the default is to set its display by Wrap_content, which is only explicitly declared in the GridLayout layout.
To specify that a control is displayed in a fixed row or column, simply set the Android:layout_row and Android:layout_column properties of the child control, but be aware that android:layout_row= "0" means starting from the first row, android:layout_column= "0" means starting from the first column, which is similar to the assignment of a one-dimensional array in a programming language.
If you need to set a control to span multiple rows or columns, simply set the Android:layout_rowspan or Layout_columnspan property of the child control to a number, and then set its Layout_gravity property to fill. The previous setting indicates the number of rows or columns that the control spans, and the next setting indicates that the control fills the entire row or column that spans.

Take a look at the six major Android layouts

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.