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>