On Android five layouts (ii)--relativelayout and tablelayout "Turn"

Source: Internet
Author: User

Http://www.cnblogs.com/wisekingokok/archive/2011/08/24/2152004.html

The three layouts of linearlayout (linear layout), Framelayout (single frame layout) and absoultelayout (absolute layout) have been described in the five major Android layouts (i). The remaining two layouts relativelayout (relative layout) and tablelayout (table layout) are slightly more complex than the previous layout structure, so here is another space.

Relativelayout:

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:layout_toleftof--the component is on the left side of the reference component
android:layout_torightof--the component is on the right 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.

1 Relativelayout2<?xml version= "1.0" encoding= "Utf-8"?>3<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >4<textview android:id= "@+id/text_01" android:layout_width= "50DP" android:layout_height= "50DP" Android:background = "#ffffffff" android:gravity= "center" android:layout_alignparentbottom= "true" android:text= "1"/>5<textview android:id= "@+id/text_02" android:layout_width= "50DP" android:layout_height= "50DP" Android:background = "#ff654321" android:gravity= "center" android:layout_above= "@id/text_01" android:layout_centerhorizontal= "true" android:text= "2"/>6<textview android:id= "@+id/text_03" android:layout_width= "50DP" android:layout_height= "50DP" Android:background = "#fffedcba" android:gravity= "center" android:layout_toleftof= "@id/text_02" android:layout_above= "@id/text_01" android:text= "3"/>7</RelativeLayout>
View Code

Tablelayout:

  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.

The following example shows a tablelayout layout structure where the second row has only two cells and the remaining rows are three cells.

1 Tablelayout2<?xml version= "1.0" encoding= "Utf-8"?>3<tablelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "Vertical" Android : layout_width= "fill_parent" android:layout_height= "Fill_parent" >4<tablerow android:layout_width= "fill_parent" android:layout_height= "Wrap_content" >5<textview android:background= "#ffffffff" android:gravity= "center" android:padding= "10DP" android:text= "1"/>6<textview android:background= "#ff654321" android:gravity= "center" android:padding= "10DP" android:text= "2"/>7<textview android:background= "#fffedcba" android:gravity= "center" android:padding= "10DP" android:text= "3"/>8</TableRow>9<tablerow android:layout_width= "fill_parent" android:layout_height= "Wrap_content" >Ten<textview android:background= "#ff654321" android:gravity= "center" android:padding= "10DP" android:text= "2"/> One<textview android:background= "#fffedcba" android:gravity= "center" android:padding= "10DP" android:text= "3"/> A</TableRow> -<tablerow android:layout_width= "fill_parent" android:layout_height= "Wrap_content" > -<textview android:background= "#fffedcba" android:gravity= "center" android:padding= "10DP" android:text= "3"/> the<textview android:background= "#ff654321" android:gravity= "center" android:padding= "10DP" android:text= "2"/> -<textview android:background= "#ffffffff" android:gravity= "center" android:padding= "10DP" android:text= "1"/> -</TableRow> -</TableLayout>
View Code

On Android five layouts (ii)--relativelayout and tablelayout "Turn"

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.