[Android]使用二维表格实现 TableLayout 跨行(转)

 转自: http://iwangpeng.com/2010/11/28/android-tablelayout-rowspan/

建议观看原作者网站。

Android 中提供了 TableLayout 布局,和我们平时在网页上见到的 Table 有所不同,TableLayout 没有边框,它是由多个 TableRow 对象组成,每个 TableRow 可以有0个或多个单元格,每个单元格就是一个 View

单元格可以为empty,并且通过 android:layout_column 可以设置 index 值实现跳开某些单元格,android:layout_span 可以设置合并几个单元格,即跨列。但并没有选项可以实现跨行,要想实现只能另找方法。

下面使用二维表格来实现跨行。

先看效果,其中“盘龙”封面图片跨了5行:

bookinfo.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
xml version = "1.0" encoding = "utf-8" ?>
< ScrollView
     xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:orientation = "vertical"
     >
     < TableLayout
         android:orientation = "vertical"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         >
         < TableRow android:gravity = "center"
             android:layout_marginTop = "8dip"
             >
             < ImageView
                 android:src = "@drawable/frontcover"
                 android:layout_width = "84dip"
                 android:layout_height = "105dip"
                 />
             < TableLayout
                 android:orientation = "vertical"
                 android:layout_width = "fill_parent"
                 android:layout_height = "fill_parent"
                 android:layout_marginLeft = "5dip"
                 >
                 < TableRow >
                     < TextView
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = "@string/bookName"
                         />
                     < TextView android:id = "@+id/tvBookName"
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = ""
                         />
                 TableRow >
                 < TableRow >
                     < TextView
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = "@string/bookAuthor"
                         />
                     < TextView android:id = "@+id/tvBookAuthor"
                         android:layout_width = "fill_parent"
                         android:layout_height = "21dip"
                         android:text = ""
                         />
                 TableRow >
                 < TableRow >
                     < TextView
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = "@string/bookType" />
                     < TextView android:id = "@+id/tvBookType"
                         android:layout_width = "fill_parent"
                         android:layout_height = "21dip"
                         android:text = ""
                         />
                 TableRow >
                 < TableRow >
                     < TextView
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = "@string/bookWords" />
                     < TextView android:id = "@+id/tvBookWords"
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = ""
                         />
                 TableRow >
                 < TableRow >
                     < TextView
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = "@string/bookTotalPages"
                         />
                     < TextView android:id = "@+id/tvBookTotalPages"
                         android:layout_width = "wrap_content"
                         android:layout_height = "21dip"
                         android:text = ""
                         />
                 TableRow >
             TableLayout >
         TableRow >
         < TextView
             android:layout_width = "fill_parent"
             android:layout_height = "wrap_content"
             android:text = "@string/bookDesc"
             android:layout_marginTop = "8dip"
             />
         < TextView android:id = "@+id/tvBookDesc"
             android:layout_width = "fill_parent"
             android:layout_height = "wrap_content"
             android:text = ""
             />
         < Button android:id = "@+id/btnRead"
             android:layout_width = "fill_parent"
             android:layout_height = "wrap_content"
             android:layout_marginTop = "3dip"
             android:text = "@string/startRead"
             />
     TableLayout >
ScrollView >

你可能感兴趣的:(OS——Android)