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

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


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

重要的几个属性以下:

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

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


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

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

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

补充: htm

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

②不过子对象能够定义 layout_height 属性;其默认值是WRAP_CONTENT若是子对象是 TableRow,其高度永远是 WRAP_CONTENT
索引


实例: get



<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>
相关文章
相关标签/搜索