Android Layout Daquan

Source: Internet
Author: User

Android Layout Daquan

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.

All layouts can be categorized as 5 categories of ViewGroup, 5 direct subclasses of ViewGroup. Some of the other layouts are extended from these 5 classes.

1.linearlayout, linear layout mode

This kind of layout is more commonly used, it is also relatively simple, that is, each element occupies a row, of course, may also be declared as horizontal emissions, that is, each element occupies a column.

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 only a text box, then its default value is 0, if there are two equal-length text boxes in a row, then 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.

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
<textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:background= "# ff000000 "android:text=" @string/hello "/>
<linearlayout android:orientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "Fill_ Parent ">
<textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:background= "# ff654321 "android:layout_weight=" 1 "android:text=" 1 "/>
<textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:background= "# FFFEDCBA "android:layout_weight=" 2 " android:text=" 2 "/>
</LinearLayout>
</LinearLayout>

2.Relative Layout, Relative 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, android:layout_centervertical and so on. Note When you specify a location relationship, the ID of the reference must be defined before the reference, or an exception will occur.

Relativelayout is one of the most flexible layout structures in the Android five layout structure, and is more suitable for some complex interface layouts.

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
<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"/>
<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"/>
<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"/>
</RelativeLayout>

3.Absolutelayout, 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. 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.

<?xml version= "1.0" encoding= "Utf-8"?>
<absolutelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" >
<textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#ffffffff" Android: gravity= "center" android:layout_x= "50DP" android:layout_y= "50DP" android:text= "1"/>
<textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#ff654321" Android: gravity= "center" android:layout_x= "25DP" android:layout_y= "25DP" android:text= "2"/>
<textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#fffedcba" Android: gravity= "center" android:layout_x= "125DP" android:layout_y= "125DP" android:text= "3"/>
</AbsoluteLayout>

4.FrameLayout, Frame layout
Framelayout is the simplest layout in the five layouts, which can be described as a layer layout. In this layout, the entire interface is treated as a blank alternate area where all the child elements cannot be placed, 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. As below, the first TextView is completely obscured by the second TextView, and the third TextView blocks the part of the second TextView.

<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "Vertical" Android : layout_width= "fill_parent" android:layout_height= "Fill_parent" >
<textview android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:background= "#ff000000 "android:gravity=" Center "android:text=" 1 "/>
<textview android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:background= "#ff654321 "android:gravity=" center "android:text=" 2 "/>
<textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#fffedcba" Android: gravity= "Center" android:text= "3"/>
</FrameLayout>

5.TableLayout, table layout

The layout format that applies to n rows n columns. A tablelayout is made up of many TableRow, and a tablerow represents a row in tablelayout.

TableRow is a subclass of LinearLayout, Ablellayout does not need to explicitly declare how many rows, how many columns, but TableRow, and other components to control the number of rows and columns of the table, TableRow is also a container, So you can add other components to TableRow and add a column to the table without adding a component. If you want to add a component to the Tablelayout, the component takes up a row directly. In a table layout, the width of the column is determined by the widest cell in the column, and the width of the entire table layout depends on the width of the parent container (which, by default, fills the parent container itself).

Tablelayout inherits LinearLayout, so he can fully support all the XML attributes supported by LinearLayout, in addition to the following properties Tablelayout support:

XML attribute related usage description

1. Andriod:collapsecolumns setcolumnscollapsed (int, Boolean) sets the sequence number of the column that needs to be hidden, with multiple comma-separated

2.android:shrinkcolumns Setshrinkallcolumns (Boolean) sets the serial number of the column that is shrunk, with multiple comma-separated

3.android:stretchcolimns Setsretchallcolumnds (Boolean) sets the sequence number of columns that are allowed to be stretched, separated by commas

<?xml version= "1.0" encoding= "Utf-8"?>
<tablelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "Vertical" Android : layout_width= "fill_parent" android:layout_height= "Fill_parent" >
<tablerow android:layout_width= "fill_parent" android:layout_height= "Wrap_content" >
<textview android:background= "#ffffffff" android:gravity= "center" android:padding= "10DP" android:text= "1"/>
<textview android:background= "#ff654321" android:gravity= "center" android:padding= "10DP" android:text= "2"/>
<textview android:background= "#fffedcba" android:gravity= "center" android:padding= "10DP" android:text= "3"/>
</TableRow>
<tablerow android:layout_width= "fill_parent" android:layout_height= "Wrap_content" >
<textview android:background= "#ff654321" android:gravity= "center" android:padding= "10DP" android:text= "2"/>
<textview android:background= "#fffedcba" android:gravity= "center" android:padding= "10DP" android:text= "3"/>
</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"/>
<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>

6. Other layouts (see the affiliation relationship)

1) List view

List view is a scrollable listing. Displays specific content as a list, and can be displayed adaptively based on the length of the data.

For specific applications See: Usage one http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

Usage two http://blog.csdn.net/koupoo/article/details/7018727

2) grid Views (grid view)

Grid View a viewgroup displays its child view elements in a grid, which is a two-dimensional, scrolling grid.

Specific applications View: http://www.cnblogs.com/linzheng/archive/2011/01/19/1938760.html

3) Label layouts (tab layout)

Displays its child view elements as a label, just as it would appear in a window in Firefox. To create a label UI (tabbed UI) for madness, you need to use the TabHostTabWidget。TabHost必须是布局的根节点,它包含为了显示标签的TabWidget和显示标签内容的FrameLayout。

Specific applications View: http://www.cnblogs.com/devinzhang/archive/2012/01/18/2325887.html

Android Layout Daquan

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.