Original Address http://blog.csdn.net/jianghuiquan/article/details/8299970
Tablelayout Table Layout
Tablelayout refers to assigning the position of a child element to a row or column. A tablelayout of Android has many TableRow, each of which defines a row TableRow. The Tablelayout container does not display row,column, and the cell's border line, each row has 0 or more cells, each with a view object.
When using tablelayout, be aware of the width of each cell.
The following are examples of the XML layout and the Java code layout, respectively:
First, the layout of XML mode
1. Create a blank activity
2. Open the "res/layout/activity_main.xml" file and modify it to the following code.
(1) Section ①
<?xml version= "1.0" encoding= "Utf-8"?, each XML document starts with the XML prologue, the first line in the preceding code is the XML preamble, <?xml version= "1.0" >. This line of code means parsing according to the 1.0 version of the XML rule. encoding = "Utf-8" indicates that the XML file is in UTF-8 encoded format. The encoding format can also be GB2312.
(2) Section ②
<linearlayout ... Represents the use of the table layout manager.
(3) Section ③
Android:layout_width= "Match_parent" android:layout_height= "match_parent" means that the layout manager width and high charge will fill the entire screen width and height.
(4) Section ④
Android:stretchcolumns= "1" indicates that there is free space within the 2nd column of the table layout manager where the components can be expanded.
3, insert 1 lines TableRow, a text TextView, 1 TextEdit.
4. Open the "res/layout/activity_main.xml" file and modify it to the following code.
(1) Section ①
<TableRow></TableRow> represents a row in which the control can be populated.
(2) Section ②
Add a label <TextView>.
(3) Section ③
Add an edit box <EditText>.
5, then insert 2 lines <TableRow>, password tag <TextView>, password edit box <EditText>, 2 buttons button: Register, login.
The code is as follows:
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <tablelayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:stretchcolumns="1">
- First line
- <TableRow
- android:id="@+id/tablerow1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/tvusername"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="user name:" />
- <EditText
- android:id="@+id/etusername"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="ten" >
- <requestfocus />
- </EditText>
- </TableRow>
- Second line
- <TableRow
- android:id="@+id/tablerow2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <TextView
- android:text="Login password:"
- android:textstyle="bold"
- android:gravity="right"
- android:padding="3DP" />
- <EditText
- android:id="@+id/password"
- android:password="true"
- android:padding="3DP"
- android:scrollhorizontally="true" />
- </TableRow>
- Third line
- <TableRow
- android:id="@+id/tablerow3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/cancel"
- android:text="register" />
- <Button
- android:id="@+id/login"
- android:text="login" />
- </TableRow>
- </tablelayout>
6, the final display effect is as follows:
Attached: Table layout Common Properties Introduction
(1) Determination of the number of tablelayout rows
The number of rows for a tablelayout is specified directly by the developer, that is, how many TableRow objects (or view controls) there are.
The number of columns of tablelayout equals the number of columns of TableRow that contain the most child controls. If the first TableRow contains 2 child controls, the second TableRow contains 3, and the third TableRow contains 4, then the Tablelayout has a number of 4 columns.
(2) Tablelayout the properties that can be set
Tablelayout properties that can be set include global properties and cell properties.
A) The global attribute is also a column property, with the following 3 parameters:
Android:stretchcolumns sets the columns that can be stretched. The column can stretch to the row direction, up to a full row.
Android:shrinkcolumns sets the columns that can be shrunk. When the column control has too much content and is already packed in the row, the contents of the child control are displayed in the columns direction.
Android:collapsecolumns sets the columns to hide.
Example:
android:stretchcolumns= "0" No. 0 column extendable
Android:shrinkcolumns= 1th, 2 columns can be shrunk
android:collapsecolumns= "*" hides all rows
Note: Columns can have both the Stretchcolumns and Shrinkcolumns properties, so if the column has more than N, "multiline" displays its contents. (This is not really multiple lines, but the system automatically adjusts the line's layout_height as needed)
b) Cell properties, with the following 2 parameters:
Android:layout_column specifies that the cell is displayed in the first column
ANDROID:LAYOUT_SPAN Specifies the number of columns that the cell occupies (when unspecified, 1)
Example:
android:layout_column= "1" The control is displayed in the 1th column
Android:layout_span= "2" This control occupies 2 columns
Description: A control can also have both features.
Ii. layout of Java code mode
Now that we've learned to use XML for linearlayout layouts, let's learn how to do the same with Java code.
Java code is temporarily abbreviated.
Layout Layouts tablelayout Table layout