本节引言:

��我们已�学习了平时实际开�中用得较多的线性布局(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就到这里~无�就是五个属性的使用而已,实际开� 表格布局我们用的�多,知�简�的用法就�以了�