Android 布局详解 -三表格布局(TableLayout)以及重要属性

来源:互联网 发布:淘宝卖的苍蝇水叫什么 编辑:程序博客网 时间:2024/06/08 17:28

三表格布局(TableLayout)以及重要属性

原文https://my.oschina.net/u/1418901/blog/184627

          TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button、TextView等控件就在TableRow之上,别的,TableLayout之上也可以零丁放控件。TableLayout是一个应用错杂的布局,最简单的用法就仅仅是拖沓控件做出个界面,但实际上,会经常在代码里应用TableLayout,例如做出表格的结果。本文首要介绍TableLayout的根蒂根基应用办法。        

重要的几个属性如下:

 1.android:collapseColumns://隐藏指定的列
        ①设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开 
        ②以第0行为序,隐藏指定的列:把android:collapseColumns=0,3 意思是把第0和第3列隐藏

 
    2.android:shrinkColumns://收缩指定的列以适合屏幕、不会挤出屏幕                                     ① 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多列个用“,”隔开(多列 每列填充空隙大小一样)
       
以第0行为序,自动延伸指定的列填充可用部分: 当LayoutRow里面的控件还没有布满布局时,shrinkColumns不起作用。
     ③
设置shrinkColumns=1,4,布局完全没有改变,因为LayoutRow里面还剩足够的空间。当LayoutRow布满控件时,设置了shrinkColumns=2,5,则控件自动向垂直方向填充空间 


                                                                                                                                         3.android:stretchColumns://尽量把指定的列表填充空白部分                                 

      ①设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕 外,此列会自动收缩)的列的列索引,多个用“,”隔开                                                                                                                      

         ② 以第0行为序,尽量把指定的列填充空白部分:设置stretchColumns=2,5,第1,4列被尽量填充同时向右填充,直到2,5被压挤到最后边)。 

补充:

①表格布局的子对象不能指定 layout_width 属性.宽度永远是 MATCH_PARENT

②不过子对象可以定义 layout_height 属性;其默认值是WRAP_CONTENT. 如果子对象是 TableRow,其高度永远是 WRAP_CONTENT


实例:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".AndroidTableLayoutActivity" >    <!-- 定义第一个表格,指定第2列允许收缩,第3列允许拉伸 -->    <TableLayout        android:id="@+id/tablelayout01"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:shrinkColumns="1"        android:stretchColumns="2" >        <!-- 直接添加按钮,自己占用一行 -->        <Button            android:id="@+id/btn01"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="独自一行" >        </Button>        <TableRow>            <Button                android:id="@+id/btn02"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通" >            </Button>            <Button                android:id="@+id/btn03"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="允许被收缩允许被收缩允许被收缩允许被收缩" >            </Button>            <Button                android:id="@+id/btn04"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="允许被拉伸允许被拉伸允许被拉伸" >            </Button>        </TableRow>    </TableLayout>    <!-- 定义第2个表格,指定第2列隐藏 -->    <TableLayout        android:id="@+id/tablelayout02"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:collapseColumns="1" >        <TableRow>            <Button                android:id="@+id/btn05"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通" >            </Button>            <Button                android:id="@+id/btn06"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="被隐藏列" >            </Button>            <Button                android:id="@+id/btn07"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="允许被拉伸" >            </Button>        </TableRow>    </TableLayout>    <!-- 定义第3个表格,指定第2列填满空白 -->    <TableLayout        android:id="@+id/tablelayout03"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:stretchColumns="1" >        <TableRow>            <Button                android:id="@+id/btn08"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通" >            </Button>            <Button                android:id="@+id/btn09"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="填满剩余空白" >            </Button>        </TableRow>    </TableLayout>    <!-- 定义第3个表格,指定第2列横跨2列 -->    <TableLayout        android:id="@+id/tablelayout04"        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <TableRow>            <Button                android:id="@+id/btn10"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通" >            </Button>            <Button                android:id="@+id/btn11"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_column="2"                android:text="填满剩余空白" >            </Button>        </TableRow>    </TableLayout></LinearLayout>
0 0
原创粉丝点击