本节引言:
å‰�é�¢æˆ‘们已ç»�å¦ä¹ 了平时实际开å�‘ä¸ç”¨å¾—较多的线性布局(LinearLayout)与相对布局(RelativeLayout), 其实å¦å®Œè¿™ä¸¤ä¸ªåŸºæœ¬å°±å¤Ÿç”¨äº†,笔者在实际开å�‘ä¸ç”¨å¾—比较多的也是这两个,当然作为一个好å¦çš„程åº�猿, éƒ½æ˜¯å–œæ¬¢åˆ¨æ ¹é—®é¢˜çš„,所以虽说用得ä¸�多,但是还是有必è¦�å¦ä¹ 一下基本的用法的,说ä¸�定哪一天能用得上呢! ä½ è¯´æ˜¯å�§,å¦å¤šç‚¹ä¸œè¥¿æ²¡ä»€ä¹ˆçš„,å�ˆä¸�å�ƒäº�ï¼�好了,扯淡就扯到这里,开始这一节的å¦ä¹ å�§,这一节我们会å¦ä¹ Androidä¸çš„第三个布局:TableLayout(è¡¨æ ¼å¸ƒå±€)ï¼�
1.本节å¦ä¹ 路线图
路线图分æž�: 从上é�¢çš„路线图,å�¯ä»¥çœ‹å‡ºTableLayout的用法还是很简å�•çš„,æ— é�žå°±æ˜¯ç¡®å®šè¡¨æ ¼çš„行数,以å�Šä½¿ç”¨ 那三个属性æ�¥è®¾ç½®æ¯�一行ä¸çš„第æŸ�åˆ—çš„å…ƒç´ éš�è—�,拉伸,或者收缩å�³å�¯ï¼�
2.TableLayout的介�
相信å¦è¿‡HTML的朋å�‹éƒ½çŸ¥é�“,我们å�¯ä»¥é€šè¿‡< table >< tr >< td >å°±å�¯ä»¥ç”Ÿæˆ�一个HTMLçš„è¡¨æ ¼, 而Androidä¸ä¹Ÿå…�è®¸æˆ‘ä»¬ä½¿ç”¨è¡¨æ ¼çš„æ–¹å¼�æ�¥æŽ’列组件,就是行与列的方å¼�,就说我们这节的TableLayout! 但å�´ä¸�åƒ�我们å�Žé�¢ä¼šè®²åˆ°çš„Android 4.0å�Žå¼•入的GridLayout(ç½‘æ ¼)å¸ƒå±€ä¸€æ ·,直接就å�¯ä»¥è®¾ç½®å¤šå°‘行与多少列ï¼�
3.如何确定行数与列数
- ①如果我们直接往TableLayout䏿·»åŠ ç»„ä»¶çš„è¯�,那么这个组件将å� 满一行ï¼�ï¼�ï¼�
- ②如果我们想一行上有多个组件的è¯�,å°±è¦�æ·»åŠ ä¸€ä¸ªTableRow的容器,把组件都丢到里é�¢ï¼�
- â‘¢tablerowä¸çš„组件个数就决定了该行有多少列,è€Œåˆ—çš„å®½åº¦ç”±è¯¥åˆ—ä¸æœ€å®½çš„å�•å…ƒæ ¼å†³å®š
- ④tablerow的layout_width属性,默认是fill_parent的,我们自己设置�其他的值也�会生效��� 但是layout_height默认是wrapten——content的,我们��以自己设置大��
- â‘¤æ•´ä¸ªè¡¨æ ¼å¸ƒå±€çš„å®½åº¦å�–决于父容器的宽度(å� 满父容器本身)
- ⑥有多少行就è¦�自己数啦,一个tablerow一行,一个å�•独的组件也一行ï¼�多少列则是看tableRowä¸ çš„ç»„ä»¶ä¸ªæ•°,组件最多的就是TableLayout的列数
4.三个常用属性
android:collapseColumns:设置需�被��的列的��
android:shrinkColumns:设置�许被收缩的列的列��
android:stretchColumns:设置�行被拉伸的列的列��以上这三个属性的列�都是从0开始算的,比如shrinkColunmns = "2",对应的是第三列�
�以设置多个,用逗�隔开比如"0,2",如果是所有列都生效,则用"*"���
除了这三个常用属性,还有两个属性,åˆ†åˆ«å°±æ˜¯è·³æ ¼å�以å�Šå�ˆå¹¶å�•å…ƒæ ¼,这和HTMLä¸çš„Table类似:android:layout_column="2":表示的就是跳过第二个,ç›´æŽ¥æ˜¾ç¤ºåˆ°ç¬¬ä¸‰ä¸ªæ ¼å�处,从1开始算的!
android:layout_span="4":表示å�ˆå¹¶4个å�•å…ƒæ ¼,也就说这个组件å� 4个å�•å…ƒæ ¼
属性使用示例:
①collapseColumns(��列)
æµ�程:在TableRowä¸å®šä¹‰5个按钮å�Ž,接ç�€åœ¨æœ€å¤–层的TableLayout䏿·»åŠ ä»¥ä¸‹å±žæ€§: android:collapseColumns = "0,2",就是éš�è—�第一与第三列,代ç �如下:
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="0,2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
</TableRow>
</TableLayout>
�行效果图:
②stretchColumns(拉伸列)
æµ�程:在TableLayoutä¸è®¾ç½®äº†å››ä¸ªæŒ‰é’®,接ç�€åœ¨æœ€å¤–层的TableLayout䏿·»åŠ ä»¥ä¸‹å±žæ€§: android:stretchColumns = "1"
设置第二列为å�¯æ‹‰ä¼¸åˆ—,让该列填满这一行所有的剩余空间,代ç �如下:
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</TableRow>
</TableLayout>
�行效果图:
③shrinkColumns(收缩列)
æ¥éª¤:这里为了演示出效果,设置了5个按钮和一个文本框,在最外层的TableLayout䏿·»åŠ ä»¥ä¸‹å±žæ€§: android:shrinkColumns = "1"
设置第二个列为å�¯æ”¶ç¼©åˆ—,代ç �如下:
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本XX" />
</TableRow>
</TableLayout>
�行截图:
ä»Žå›¾ä¸æˆ‘们å�¯ä»¥çœ‹åˆ°two这个按钮被挤压æˆ�æ�¡æ�¡çж,这个就是收缩,为了ä¿�è¯�è¡¨æ ¼èƒ½é€‚åº” 父容器的宽度ï¼�至于å�¦å¤–两个属性就ä¸�讲解了,用法和HTML相å�Œï¼�有兴趣的å�¯ä»¥ç ”究下ï¼�
5.使用实例
使用TableLayout�完�简�的登录界�,�行效果图如下:
�程解�:
①调用gravity属性,设置为center_vertical,让布局里é�¢çš„组件在竖直方å�‘上居ä¸
â‘¡å°†TableLayoutä¸çš„第一和第四列设置为å�¯æ‹‰ä¼¸
③在æ¯�个TableRow䏿·»åŠ ä¸¤ä¸ªTextView,用于拉伸填满该行,è¿™æ ·å�¯ä»¥è®©è¡¨æ ¼æ°´å¹³å±…ä¸
android:stretchColumns="0,3" 设置为0.3,是为了让两边都充满,那么ä¸é—´éƒ¨åˆ†å°±å�¯ä»¥å±…ä¸äº†
详细代ç �如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/TableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:stretchColumns="0,3" android:gravity="center_vertical" android:background="#66FF66" > <TableRow> <TextView /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户å��:"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="150dp"/> <TextView /> </TableRow> <TableRow> <TextView /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 ç �:" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="150dp" /> <TextView /> </TableRow> <TableRow> <TextView /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出"/> <TextView /> </TableRow> </TableLayout>
6.�现的问题
相信大家在使用这个这TableLayoutçš„TableRow的时候会é�‡åˆ°è¿™ä¸ªè¦å‘Š:
当然,程åº�还是å�¯ä»¥è¿�行的,ä¸�è¿‡æˆ–è®¸ä½ æ˜¯å¼ºè¿«ç—‡æ‚£è€…,看到黄色感å�¹å�·ä½ å°±ä¸�爽的è¯�ï¼� 而解决这个è¦å‘Šçš„æ–¹æ³•也是很奇葩的:å�ªè¦�ä½ çš„TableLayout里é�¢æœ‰2个或以上的TableRowå°±å�¯ä»¥äº†ï¼�
本节�结:
好的,关于Android的第三个布局:TableLayout就到这里~æ— é�žå°±æ˜¯äº”个属性的使用而已,实际开å�‘ è¡¨æ ¼å¸ƒå±€æˆ‘ä»¬ç”¨çš„ä¸�多,知é�“简å�•的用法就å�¯ä»¥äº†ï¼�