Chapter 3 UI cornerstone-UI layout (4) and ui cornerstone
3.2.5 form layout (TableLayout)
TableLayout is a form layout that manages controls in the form of rows and columns. Each behavior is a TableRow object, which can also be a View object. When it is a View object, the object will span all columns of the row.
We also use a simple example to illustrate it. After reading the example, I believe you will have a comprehensive understanding of the TableLayout application.
<? Xml version = "1.0" encoding = "UTF-8"?> <TableLayout xmlns: android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: stretchColumns = "0, 1, 2" Android: shrinkColumns = "1, 2"> <TextView Android: text = "TableLayout" Android: gravity = "center"/> <TableRow> <TextView Android: layout_column = "1" Android: text = "name" Android: gravity = "center"/> <TextView Android: text = "gender" Android: gravity = "center"/> </TableRow> <TableRow> <TextView Android: text = "1" Android: gravity = "center"/> <TextView Android: text = "zhangsan" Android: gravity = "left"/> <TextView Android: text = "male" Android: gravity = "center"/> </TableRow> <TableRow> <TextView Android: text = "2" Android: gravity = "center"/> <TextView Android: text = "lilei" Android: gravity = "left"/> <TextView Android: text = "male" Android: gravity = "center"/> </TableRow> <TableRow> <TextView Android: text = "3" Android: gravity = "center"/> <TextView Android: text = "hanmeimei" Android: gravity = "left"/> <TextView Android: text = "female" Android: gravity = "center"/> </TableRow> </TableLayout> |
As shown in figure 3-8.
Figure 3-8tablelayout form layout
In the TableLayout layout, there are several attributes that are commonly used. You should be familiar with them (the subscript starts from 0 ):
1) for android: stretchColumns = ", 2", set columns 1, 2, and 3 as extensible columns. After setting, these columns will fill up the remaining spaces;
2) for android: shrinkColumns = "1, 2", set columns 2 and 3 to shrink columns. After setting, these columns are automatically extended and filled with available parts;
3) android: collapse = "0", set column 1st to hide.
3.2.6 absolute layout (AbsoluteLayout)
AbsoluteLayout (absolute layout), also known as coordinate layout, is flexible and complex in layout. In addition, due to the differences in the screen sizes of various mobile phones, our development is also more difficult.
Similarly, we will give you a simple example to help you master it.
<? Xml version = "1.0" encoding = "UTF-8"?> <AbsoluteLayout Xmlns: android = "http://schemas.android.com/apk/res/android" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <ImageView Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/ball" Android: layout_x = "45px" Android: layout_y = "60px"/> <TextView Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "absolutelayout" Android: textColor = "@ android: color/black" Android: layout_x = "100px" Android: layout_y = "175px"/> <TextView Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "absolutelayout" Android: textColor = "@ android: color/black" Android: layout_x = "120px" Android: layout_y = "195px"/> <TextView Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "absolutelayout" Android: textColor = "@ android: color/black" Android: layout_x = "140px" Android: layout_y = "215px"/> </AbsoluteLayout> |
As shown in figure 3-9.
Figure 3-9absolutelayout absolute Layout
When using this layout, We need to calculate the size and position of each view, and then set their positions through the android: layout_x and android: layout_y attributes.
Experience Sharing: Because the screen resolution and size of the current Android device are very different, absolute layout is no longer used in the development process, you can use other layout methods. If you do need to use absolute layout, consider the following: 1) The coordinate origin is the upper left corner of the screen; 2) When adding a view, you must accurately calculate the pixel size of each view. It is best to first draw a sketch on the paper and calculate the pixel location of all elements. |