Five la s of Android (2) -- relativelayout and tablelayout

Source: Internet
Author: User

Talking about the five layout s of Android (1), we have described three layout structures: linearlayout (linear layout), framelayout (single frame layout), and absoultelayout (absolute layout, the remaining two la S, relativelayout (relative layout) and tablelayout (table layout), are a little more complex than the previous layout structure, so we will introduce them in another article.

Relativelayout:

Relativelayout completes the Layout Based on the positional relationship between child elements. The location-related attributes in the child element in this layout take effect. For example, Android: layout_below and Android: layout_abve. The child element uses these attributes and their IDs to specify the location relationship. Note: When specifying the location relationship, the referenced ID must be defined before the reference; otherwise, an exception occurs.

Common location attributes in relativelayout are as follows:
Android: layout_toleftof -- This component is located on the left of the referenced component
Android: layout_torightof -- This component is located on the right of the referenced component
Android: layout_above -- the component is located above the referenced component
Android: layout_below -- the component is located below the referenced component
Android: layout_alignparentleft -- whether the component is aligned with the left end of the parent component
Android: layout_alignparentright -- whether the component is the right end of its parent component
Android: layout_alignparenttop -- whether the component is aligned with the top of the parent component
Android: layout_alignparentbottom -- whether the component is aligned with 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 horizontally centered
Android: layout_centervertical -- whether the component is vertically centered

Relativelayout is the most flexible layout structure in the Five layout structures of Android, which is suitable for layout of some complex interfaces. The following example shows that the first text box is aligned with the bottom of the parent component, the second text box is located above the first text box, and the third text box is located at the left of the second text box.

Relativelayout

 1  <?  XML version = "1.0" encoding = "UTF-8"  ?>  
2 < Relativelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: Orientation = "Vertical" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" >
3 < 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" />
4 < Textview Android: ID = "@ + ID/text_02" Android: layout_width = "50dp" Android: layout_height = "50dp" Android: Background = "# Ff654321" Android: gravity = "Center" Android: layout_abve = "@ ID/text_01" Android: layout_centerhorizontal = "True" Android: Text = "2" />
5 < 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_abve = "@ ID/text_01" Android: Text = "3" />
6 </ Relativelayout >

 

Tablelayout:

Tablelayout, as its name implies, is a table layout. It is applicable to the layout format of N rows and n columns. A tablelayout consists of many tablerow. A tablerow represents a row in tablelayout.

Tablerow is a subclass of linearlayout. Its Android: Orientation attribute value is always horizontal, and its Android: layout_width and Android: layout_height attributes are always match_parent and wrap_content. Therefore, its child elements are arranged horizontally and the width and height are consistent. This design makes the sub-elements in each tablerow 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. The second row has only two cells, and the other rows have three cells.

Tablelayout

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  
2 < Tablelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android" Android: Orientation = "Vertical" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" >
3 < Tablerow Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" >
4 < Textview Android: Background = "# Ffffffff" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "1" />
5 < Textview Android: Background = "# Ff654321" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "2" />
6 < Textview Android: Background = "# Fffedcba" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "3" />
7 </ Tablerow >
8 < Tablerow Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" >
9 < Textview Android: Background = "# Ff654321" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "2" />
10 < Textview Android: Background = "# Fffedcba" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "3" />
11 </ Tablerow >
12 < Tablerow Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" >
13 < Textview Android: Background = "# Fffedcba" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "3" />
14 < Textview Android: Background = "# Ff654321" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "2" />
15 < Textview Android: Background = "# Ffffffff" Android: gravity = "Center" Android: padding = "10dp" Android: Text = "1" />
16 </ Tablerow >
17 </ Tablelayout >

 

For other layout structures, see:Talking about the five la s of Android (1)-relativelayout and talbelayout.

========================================================== ======================================
If you think this blog is useful to you, do not use your mouse to give this blog a "recommendation.

This blog post is original and copyrighted. If you need to reprint it, please enter the reposted address.
Original blog address: wisekingokok.cnblogs.com
========================================================== ======================================

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.