Five types of Android layouts

Source: Internet
Author: User

1. LinearLayout

LinearLayout is a linear layout control that contains child controls that are arranged horizontally or vertically.

Note: Layouts can be nested between the use. That is, the layout can contain both controls and layouts.

Two Global properties:

1. Android:orientation--determines its sub-class control arrangement Android:orientation=--horizontal arrangement Android:orientation=" vertical "--vertical arrangement Span class= "Hljs-number" >2. Android:gravity --determines the sub-class control position Android:gravity= "bottom" --bottom Android:gravity= "Bottom|center_horizontal" --label can be used (note: Vertical bar can not add space, need to pay attention to logic) --gravity several property values: Center_ Horizontal --Horizontal center center_vertical --Vertical Center center --Horizontal vertical is centered right --sub-class control is located to the right of the current layout left --sub-class control is located on the left side of the current layout bottom --bottom      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

Child controls Common Properties:

1. android:layout_gravity= "bottom"  --控件本身在当期父容器的位置2. android:layout_weight= "n" --指本身控件占当前父容器的一个比例(n为数字)
    • 1
    • 2

Weight Property Sample code:

<edittext Android:id= "@+id/text_1" android:layout_width=  "0DP" --note here is 0DP (do not forget unit) Android:layout_weight=" 1 "--weight android:layout_height=" Wrap_ Content "/><button android:id=" @+id/button1 "Android: Layout_width= "0DP" --note here is 0DP (do not forget unit) android:layout_height=< Span class= "hljs-string" > "wrap_content" Android:layout_weight= "1" --weight android:text= "send"/>  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

Note: The Layout_width property of the two controls is "0DP" and also indicates that the Layout_width property does not work when the Layout_weight property is available. The designation of 0 here is a comparison specification.

Effect:

Note: The system first adds the layout_weight specified by all controls under LinearLayout to get a total value, and then the ratio of the size of each control is divided by the value of the control's layout_weight.

If the code changes to:

<EditText     android:id="@+id/text_1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_weight="1" /><Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send" />
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

The effect is as follows:

This looks more comfortable and has a better fit on various screens.

Attention:
1. Gravity is used to specify how the text is aligned in the control, whereas layout_gravity is used to specify how the control is aligned in the layout.
2. When the linearlayout direction is horizontal, only alignment in the vertical direction will take effect. Because the length in the horizontal direction is not fixed at this point, the length in the horizontal direction changes each time you add a control, so you cannot specify alignment in that direction. Similarly, when the linearlayout is arranged in a vertical direction, only the horizontal alignment will take effect.

2. Relativelayout

Relativelayout is a relative layout control that contains child controls that are arranged in a relative position between controls or as a subclass control relative to the position of the parent class container.
The subclass control is relative to the parent class container:

android:layout_margin="40DP"--child controls margins from parent containerandroid:layout_marginleft= "32DP"--child controls from the left margin of the parent container android:layout_margintop=" 40DP "--child controls from the parent container's top margin  "true"--child control is relative to the current parent container on the left android : Layout_alignparentright= "true" android:layout_ Alignparenttop= "true" android:layout_alignparentbottom= "true" android:layout_centerhorizontal= " True "--child controls are centered horizontally relative to the parent container android:layout_centervertical=" true " Span class= "Hljs-label" >android:layout_centerinparent= "true"--horizontally and vertically centered   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

Sub-class control relative to child class control:

android: Layout_below= "@+id/bt1"--android:layout_above= "@+id/bt1"--Above android:layout_torightof=android:layout_alignbaseline= "@+id/bt1"-- The control is on a line with the content of the given control android:layout_aligntop= "@+id/bt1"-- The control aligns with the top of the given control android:layout_alignbottom= "@+id/bt1" android:layout_alignleft= "@+id/bt1"-the control is aligned with the left edge of the given control android:layout_alignright= "@+id/bt1"     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
3. Framelayout

Framelayout is a frame layout in which all child elements cannot be placed at the specified position, which is placed by default in the upper-left corner of the layout. And the subsequent child elements are directly above the preceding child elements, which partially or completely obscures the preceding child elements.

Several properties:

android:foreground="" --设置前景图 ,在所有子视图的前面android:foregroundGravity="" --设置前景图位置android:layout_gravity="center" --设置居中android:background= "#999999" --设置背景android:keepScreenOn="true" --保持屏幕唤醒
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
4. Tablelayout

The Tablelayout table layout model manages child controls in the form of rows and columns, each of which behaves as a TableRow object, or it can be a view object.


Tablelayout Global Properties:

android:collapseColumns="n" --隐藏第 n 列android:stretchColumns= "m, n" --拉伸第 m,n 列android:shrinkColumns= "n" --压缩第 n 列
    • 1
    • 2
    • 3

Note: The index starts at 0 and is separated by commas (* denotes all columns)

Tablelayout Local Properties:

"n" --该控件占据第 n 列(注意:从第0列开始算起)android:layout_span= "n" --该控件占用 n 列
    • 1
    • 2

Note: Controls in TableRow cannot specify a width.

5. Absolutelayout

Absolutelayout is an absolute layout, also called a coordinate layout, that directly specifies the absolute position (XY coordinates) of the child element. Because the mobile phone screen size difference is relatively large, so the absolute layout of poor adaptability. Therefore, this layout is used less. Understand.

Five types of 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.