Layout Layouts tablelayout Table layout

Source: Internet
Author: User

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <tablelayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:stretchcolumns="1">
  7. First line
  8. <TableRow
  9. android:id="@+id/tablerow1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" >
  12. <TextView
  13. android:id="@+id/tvusername"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="user name:" />
  17. <EditText
  18. android:id="@+id/etusername"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:ems="ten" >
  22. <requestfocus />
  23. </EditText>
  24. </TableRow>
  25. Second line
  26. <TableRow
  27. android:id="@+id/tablerow2"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content" >
  30. <TextView
  31. android:text="Login password:"
  32. android:textstyle="bold"
  33. android:gravity="right"
  34. android:padding="3DP" />
  35. <EditText
  36. android:id="@+id/password"
  37. android:password="true"
  38. android:padding="3DP"
  39. android:scrollhorizontally="true" />
  40. </TableRow>
  41. Third line
  42. <TableRow
  43. android:id="@+id/tablerow3"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content" >
  46. <Button
  47. android:id="@+id/cancel"
  48. android:text="register" />
  49. <Button
  50. android:id="@+id/login"
  51. android:text="login" />
  52. </TableRow>
  53. </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

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.