TableLayout和我们平时在网页上见到的Table有所不同,TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能设置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定义,默认是wrap_content。
单元格可以为empty,并且通过android:layout_column可以设置index值实现跳开某些单元格。在TableRow之间,添加View,设置layout_height以及背景色,就可以实现一条间隔线。android:layout_span可以设置合并几个单元格:
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent">
5.
6. <TableRow>
7. <TextView
8. android:text="column1"
9. android:padding="3dip" />
10. <TextView
11. android:text="column2"
12. android:padding="3dip" />
13. <TextView
14. android:text="column3"
15. android:padding="3dip" />
16. </TableRow>
17.
18. <TableRow>
19. <TextView
20. android:text="column11"
21. android:visibility="invisible"/> //cell不见了
22. <TextView
23. android:text="左边的invisible"
24. android:gravity="right"
25. android:padding="3dip" />
26. <Button
27. android:id="@+id/go"
28. android:text="go"
29. android:padding="3dip" />
30. <Button
31. android:text="cancel"
32. android:padding="3dip" />
33. </TableRow>
34.
35. <View //间隔线
36. android:layout_height="2dip"
37. android:background="#F00" />
38.
39. <TableRow>
40. <TextView
41. android:text="右边的cell empty" />
42. <TextView
43. android:layout_column="2"
44. android:text="跳开empty cell"
45. android:padding="3dip" />
46. </TableRow>
47.
48. <TableRow>
49. <TextView
50. android:text="合并3个单元格"
51. android:layout_span="3"
52. android:gravity="center_horizontal"
53. android:background="#FFC0C0C0"
54. android:textColor="#f00"
55. android:padding="3dip" />
56. </TableRow>
57. </TableLayout>
没有设置收缩/伸展效果
注意,原来没有添加 android:padding="3dip" 的,发现那些column会凑在一起的,没有空白间隔!明显看到,那个cancel按钮被挤到几乎看不见了!这时候需要使用android:shrinkColumns="可收缩的column",android:stretchColumns="可伸展的column"。
android:shrinkColumns和android:stretchColumns的值都是以0开始的index,但必须是string值,即用"1,2,5"来表示。可以用"*"来表示all columns。而且同一column可以同时设置为shrinkable和stretchable。
如果使用TableLayout类的setColumnShrinkable/setColumnStretchable (int columnIndex, boolean isShrinkable)就麻烦些了,需要一个一个column来设置。也可以使用TableLayout的setShrinkAllColumns/setStretchAllColumns来设置all columns。
判断这些column是否shrinkable或stretchable,可以调用isColumnShrinkable/isColumnStretchable(int columnIndex),isShrinkAllColumns()/isStretchAllColumns()。
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:shrinkColumns="0" > // 设置第一个column可收缩
6.
7. <TableRow>
8. <TextView
9. android:text="column1"
10. android:padding="3dip" />
11. <TextView
12. android:text="column2"
13. android:padding="3dip" />
14. <TextView
15. android:text="column3"
16. android:padding="3dip" />
17. </TableRow>
18.
19. <TableRow>
20. <TextView
21. android:text="column11"
22. android:visibility="invisible"/>
23. <TextView
24. android:text="左边的invisible"
25. android:gravity="right"
26. android:padding="3dip" />
27. <Button
28. android:id="@+id/go2"
29. android:text="go2"
30. android:padding="3dip" />
31. <Button
32. android:text="cancel"
33. android:padding="3dip" />
34. </TableRow>
35.
36. <View
37. android:layout_height="2dip"
38. android:background="#F00" />
39.
40. <TableRow>
41. <TextView
42. android:text="右边的cell empty" />
43. <TextView
44. android:layout_column="2"
45. android:text="跳开empty cell"
46. android:padding="3dip" />
47. <TextView
48. android:text="123456789"
49. android:padding="3dip" />
50. </TableRow>
51. </TableLayout>
可收缩column效果
现在可以看到第一个column为了让第4个column完整显示,而收缩得内容分为几行显示!
而可伸展column的效果就是在其他column可以完整显示时,该column就会伸展,占最多空间:
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:stretchColumns="1"> // 设置第二个column可伸展
6.
7. <TableRow>
8. <TextView
9. android:text="column1"
10. android:padding="3dip" />
11. <TextView
12. android:text="column2"
13. android:gravity="right"
14. android:padding="3dip" />
15. <TextView
16. android:text="column3"
17. android:padding="3dip" />
18. </TableRow>
19.
20. <TableRow>
21. <TextView
22. android:text="column1"
23. android:padding="3dip" />
24. <TextView
25. android:text="column2"
26. android:gravity="right"
27. android:padding="3dip" />
28. <TextView
29. android:text="column3"
30. android:padding="3dip" />
31. </TableRow>
32. </TableLayout>
可伸展column效果
而动态隐藏column,可以调用TableLayout.setColumnCollapsed (int columnIndex, boolean isCollapsed)来指定相应的column。另外TableLayout类的boolean isColumnCollapsed (int columnIndex)能够判断指定的column是否隐藏。
TableLayout可以用来做网页上的Form显示效果,看看官方的sample:
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:stretchColumns="1">
6.
7. <TableRow>
8. <TextView
9. android:text="@string/table_layout_10_user"
10. android:textStyle="bold"
11. android:gravity="right"
12. android:padding="3dip" />
13.
14. <EditText android:id="@+id/username"
15. android:text="@string/table_layout_10_username_text"
16. android:padding="3dip"
17. android:scrollHorizontally="true" />
18. </TableRow>
19.
20. <TableRow>
21. <TextView
22. android:text="@string/table_layout_10_password"
23. android:textStyle="bold"
24. android:gravity="right"
25. android:padding="3dip" />
26.
27. <EditText android:id="@+id/password"
28. android:text="@string/table_layout_10_password_text"
29. android:password="true"
30. android:padding="3dip"
31. android:scrollHorizontally="true" />
32. </TableRow>
33.
34. <TableRow
35. android:gravity="right">
36.
37. <Button android:id="@+id/cancel"
38. android:text="@string/table_layout_10_cancel" />
39.
40. <Button android:id="@+id/login"
41. android:text="@string/table_layout_10_login" />
42. </TableRow>
43. </TableLayout>
Form效果
单元格可以为empty,并且通过android:layout_column可以设置index值实现跳开某些单元格。在TableRow之间,添加View,设置layout_height以及背景色,就可以实现一条间隔线。android:layout_span可以设置合并几个单元格:
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent">
5.
6. <TableRow>
7. <TextView
8. android:text="column1"
9. android:padding="3dip" />
10. <TextView
11. android:text="column2"
12. android:padding="3dip" />
13. <TextView
14. android:text="column3"
15. android:padding="3dip" />
16. </TableRow>
17.
18. <TableRow>
19. <TextView
20. android:text="column11"
21. android:visibility="invisible"/> //cell不见了
22. <TextView
23. android:text="左边的invisible"
24. android:gravity="right"
25. android:padding="3dip" />
26. <Button
27. android:id="@+id/go"
28. android:text="go"
29. android:padding="3dip" />
30. <Button
31. android:text="cancel"
32. android:padding="3dip" />
33. </TableRow>
34.
35. <View //间隔线
36. android:layout_height="2dip"
37. android:background="#F00" />
38.
39. <TableRow>
40. <TextView
41. android:text="右边的cell empty" />
42. <TextView
43. android:layout_column="2"
44. android:text="跳开empty cell"
45. android:padding="3dip" />
46. </TableRow>
47.
48. <TableRow>
49. <TextView
50. android:text="合并3个单元格"
51. android:layout_span="3"
52. android:gravity="center_horizontal"
53. android:background="#FFC0C0C0"
54. android:textColor="#f00"
55. android:padding="3dip" />
56. </TableRow>
57. </TableLayout>
没有设置收缩/伸展效果

注意,原来没有添加 android:padding="3dip" 的,发现那些column会凑在一起的,没有空白间隔!明显看到,那个cancel按钮被挤到几乎看不见了!这时候需要使用android:shrinkColumns="可收缩的column",android:stretchColumns="可伸展的column"。
android:shrinkColumns和android:stretchColumns的值都是以0开始的index,但必须是string值,即用"1,2,5"来表示。可以用"*"来表示all columns。而且同一column可以同时设置为shrinkable和stretchable。
如果使用TableLayout类的setColumnShrinkable/setColumnStretchable (int columnIndex, boolean isShrinkable)就麻烦些了,需要一个一个column来设置。也可以使用TableLayout的setShrinkAllColumns/setStretchAllColumns来设置all columns。
判断这些column是否shrinkable或stretchable,可以调用isColumnShrinkable/isColumnStretchable(int columnIndex),isShrinkAllColumns()/isStretchAllColumns()。
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:shrinkColumns="0" > // 设置第一个column可收缩
6.
7. <TableRow>
8. <TextView
9. android:text="column1"
10. android:padding="3dip" />
11. <TextView
12. android:text="column2"
13. android:padding="3dip" />
14. <TextView
15. android:text="column3"
16. android:padding="3dip" />
17. </TableRow>
18.
19. <TableRow>
20. <TextView
21. android:text="column11"
22. android:visibility="invisible"/>
23. <TextView
24. android:text="左边的invisible"
25. android:gravity="right"
26. android:padding="3dip" />
27. <Button
28. android:id="@+id/go2"
29. android:text="go2"
30. android:padding="3dip" />
31. <Button
32. android:text="cancel"
33. android:padding="3dip" />
34. </TableRow>
35.
36. <View
37. android:layout_height="2dip"
38. android:background="#F00" />
39.
40. <TableRow>
41. <TextView
42. android:text="右边的cell empty" />
43. <TextView
44. android:layout_column="2"
45. android:text="跳开empty cell"
46. android:padding="3dip" />
47. <TextView
48. android:text="123456789"
49. android:padding="3dip" />
50. </TableRow>
51. </TableLayout>
可收缩column效果

现在可以看到第一个column为了让第4个column完整显示,而收缩得内容分为几行显示!
而可伸展column的效果就是在其他column可以完整显示时,该column就会伸展,占最多空间:
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:stretchColumns="1"> // 设置第二个column可伸展
6.
7. <TableRow>
8. <TextView
9. android:text="column1"
10. android:padding="3dip" />
11. <TextView
12. android:text="column2"
13. android:gravity="right"
14. android:padding="3dip" />
15. <TextView
16. android:text="column3"
17. android:padding="3dip" />
18. </TableRow>
19.
20. <TableRow>
21. <TextView
22. android:text="column1"
23. android:padding="3dip" />
24. <TextView
25. android:text="column2"
26. android:gravity="right"
27. android:padding="3dip" />
28. <TextView
29. android:text="column3"
30. android:padding="3dip" />
31. </TableRow>
32. </TableLayout>
可伸展column效果

而动态隐藏column,可以调用TableLayout.setColumnCollapsed (int columnIndex, boolean isCollapsed)来指定相应的column。另外TableLayout类的boolean isColumnCollapsed (int columnIndex)能够判断指定的column是否隐藏。
TableLayout可以用来做网页上的Form显示效果,看看官方的sample:
1. <?xml version="1.0" encoding="utf-8"?>
2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. android:stretchColumns="1">
6.
7. <TableRow>
8. <TextView
9. android:text="@string/table_layout_10_user"
10. android:textStyle="bold"
11. android:gravity="right"
12. android:padding="3dip" />
13.
14. <EditText android:id="@+id/username"
15. android:text="@string/table_layout_10_username_text"
16. android:padding="3dip"
17. android:scrollHorizontally="true" />
18. </TableRow>
19.
20. <TableRow>
21. <TextView
22. android:text="@string/table_layout_10_password"
23. android:textStyle="bold"
24. android:gravity="right"
25. android:padding="3dip" />
26.
27. <EditText android:id="@+id/password"
28. android:text="@string/table_layout_10_password_text"
29. android:password="true"
30. android:padding="3dip"
31. android:scrollHorizontally="true" />
32. </TableRow>
33.
34. <TableRow
35. android:gravity="right">
36.
37. <Button android:id="@+id/cancel"
38. android:text="@string/table_layout_10_cancel" />
39.
40. <Button android:id="@+id/login"
41. android:text="@string/table_layout_10_login" />
42. </TableRow>
43. </TableLayout>
Form效果

发表评论
-
startActivityForResult 简介
2011-03-29 15:55 1284依次打开Activity A1--A2--A3--A4 这时 ... -
startActivityForResult
2011-03-29 15:49 1146startActivityForResult 方法-- ... -
史上最全的Android的Tab与TabHost讲解
2011-03-28 11:22 1591Tab与TabHost 这就是Tab,而盛放Tab的 ... -
Android对话框
2011-03-25 11:21 1125Android 对话框(Dialog)大全 ... -
PreferenceActivity详解
2011-03-25 11:15 1442为了引入这个概率 首先从需求说起 即:现有某Activity专 ... -
TCP/UDP/HTTP
2011-03-25 11:09 1125先来一个讲TCP、UDP和HTTP ... -
9png
2011-03-25 11:08 1914今天学习了用9png图来优化横屏竖屏的UI,使用sdk自带的工 ... -
Notification
2011-03-25 11:07 939Android系统的状态栏(Status Bar)中有一个创新 ... -
一些技巧
2011-03-25 11:03 7871:查看是否有存储卡插入 String status=Envi ... -
布局像素单位
2011-03-25 11:03 827Android的layout文件中有时候可能会指定具体的单位, ... -
使用ActivityGroup来切换Activity和Layout
2011-03-25 11:02 1135在一个主界面中做Activity切换一般都会用TabActiv ... -
activitygroup
2011-03-25 11:01 1730说说tabhost和activitygroup 最近 ... -
线程
2011-03-25 11:01 1014今天在论坛上看到一些关于线程的帖子,我觉得与我理解的有些差异, ... -
类级框架
2011-03-25 11:00 744类集框架:Collection,Map,Iterator,En ... -
Intent打电话
2011-03-25 11:00 1217intent英文意思是意图,pending表示即将发生或来临的 ... -
Intent Uri
2011-03-25 10:59 1068进入联系人页面 1.Intent intent = new I ... -
Service
2011-03-25 10:59 940一、Service的概念 Service是Android程序中 ... -
Broadcast Receiver
2011-03-25 10:56 1948一、Broadcast Receiver简介 Android中 ... -
ContentProvider MIME类型
2011-03-25 10:55 1238Android程序的主要4部分 ... -
ContentProvider-1查询
2011-03-25 10:55 1237今天看了android的官方文档中ContentProvide ...
相关推荐
2. TableLayout和TableRow的XML布局文件,其中设置了边框相关的属性。 3. Java代码可能用于动态添加行或调整边框样式。 通过分析这个源码,你可以学习到如何根据需求自定义TableLayout的边框效果,以及如何灵活地...
android:text="Column 2" android:layout_column="1"/> <!-- Add more rows as needed --> </TableLayout> ``` 在这个例子中,我们创建了一个两列的表格,每列都包含一个`TextView`。`layout_column`属性定义...
在Android开发中,TableLayout是布局管理器的一种,它允许开发者创建类似电子表格的布局,非常适合展示数据或者组织内容。本篇文章将详细讲解如何利用TableLayout在Android中实现表格效果。 **1. TableLayout基本...
2. **添加TableRow**:在TableLayout内,为每行数据创建一个`<TableRow>`。TableRow可以包含多个子视图,它们将自动填充表格的列。 ```xml 列1数据"/> 列2数据"/> 列3数据"/> ``` 3. **定义列宽**:默认...
java TableLayout布局
在本教程中,我们将深入探讨`TableLayout`的使用,包括其基本概念、属性以及如何在实际项目中创建和操作表格布局。 `TableLayout`是Android `LinearLayout`的一个子类,它提供了行列结构来排列子视图,通常这些子...
2. **检查行中的数据** 检查行中的数据是否为空值,通常需要遍历行内的所有View,特别是那些用户可能输入数据的EditText。可以使用getText()方法获取文本内容,然后进行空值检查: ```java String text = ...
【TableLayout】是Android开发中的一个关键布局组件,主要用于创建表格结构的用户界面。在本视频教程中,讲师深入浅出地介绍了TableLayout的工作原理、使用方法及其在实际应用中的重要性。 TableLayout继承自...
2. **创建TableRow**:在`TableLayout`内,为每一行创建`TableRow`。每个`TableRow`可以包含多个`View`,如`TextView`、`ImageView`等,代表表格的单元格。 ```xml android:id="@+id/table_row_1"> android:id...
2. **创建内部GridView**:在每个`TableRow`中,添加一个`GridView`作为子视图。每个`GridView`将有自己的数据集和适配器。你需要为每个`GridView`创建一个自定义适配器,以便根据需求显示内容。 3. **适配器定制**...
2. **使用布局视图预览**:在XML布局文件中使用预览功能,检查TableLayout是否按预期显示。 3. **逐步调试**:逐个添加或修改属性,观察TableLayout的行为变化,找出导致问题的关键因素。 在提供的...
2. `DiagonalLayout.java`:可能是一个与TableLayout类似的布局管理器,但支持对角线方向的布局。 3. `SwingToolkit.java`:可能是一个工具类,包含了与Swing相关的实用方法,如创建窗口、组件等。 4. `TableLayout....
2. **TableRow**: TableRow是TableLayout的子元素,用于定义表格的一行。每个TableRow可以包含多个子View,这些View将作为表格的列。 3. **Span属性**: TableLayout中的每个View可以通过`android:layout_span`...
2. **对齐方式**:TableLayout支持组件的水平和垂直对齐方式,如左对齐、右对齐、居中和两端对齐。 3. **伸缩性**:通过设置行和列的伸缩属性,可以在空间不足或剩余时,自动调整组件的大小。例如,可以设置某些列...
在Android开发中,TableLayout是布局管理器的一种,它允许我们创建二维的表格形式的界面。这个组件在显示数据或创建表单时非常有用。本文将深入探讨如何利用TableLayout自动生成表格,以及相关的Android开发知识。 ...
TableLayout是Android开发中一种布局管理器,它用于在屏幕上创建表格式的布局。在这个布局中,你可以设置行和列,并让这些元素根据需要自动伸缩。TableLayout的主要优点在于其灵活性,可以方便地创建响应式界面,...
2. **属性详解**: - `android:stretchColumns`:指定哪些列应该拉伸以填满可用空间。当表格的宽度比其内容更宽时,这很有用。 - `android:shrinkColumns`:指定哪些列应该缩小以适应表格的宽度。 - `android:...
2. **ViewPager**: ViewPager是Android支持库中的一个视图组件,用于实现左右滑动切换页面的效果。它可以承载多个Fragment或者PagerAdapter子类的实例,每个Fragment代表一个独立的页面。用户通过手势滑动或使用...
2. **TableRow与View** TableRow是TableLayout的一个子类,它代表表格中的一行。在TableRow中,可以添加各种View对象,如TextView、ImageView等,它们将作为表格的单元格。每个View都可以通过设置权重或宽度来控制...
TableLayout是Android开发中一种布局管理器,它允许开发者以表格的形式组织UI元素。在Android应用设计中,TableLayout常用于展示数据或者创建有规则排列的界面,比如设置界面、菜单选项等。以下是对TableLayout的...