TableLayout,表格版面配置採用行列形式管理UI組件,TableLayout不需要明確地聲明有多少行和列,而是通過添加TableRow、其它組件來控製表格的行數、列數。
每次向TableLayout添加一個TableRow,就是在向表格添加一行,TableRow也是容器,可以向TableRow中添加組件,每添加一個組件,即是添加一列。
如果直接向TableLayout添加組件,則認為這個組件佔用一行。
表格版面配置中列的寬度即是每一列中最寬的組件的寬度。
使用前:
使用後:
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="最近連絡人"
android:id="@+id/button4"
android:layout_width="1dip"
android:layout_height="wrap_content"></Button>
<Button
android:text="連絡人"
android:id="@+id/button5"
android:layout_width="1dip"
android:layout_height="wrap_content"></Button>
<Button
android:text="分組"
android:id="@+id/button5"
android:layout_width="1dip"
android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="最近連絡人"
android:id="@+id/button4"
android:layout_width="1dip"
android:layout_height="wrap_content"></Button>
<Button
android:text="連絡人"
android:id="@+id/button5"
android:layout_width="1dip"
android:layout_height="wrap_content"></Button>
<Button
android:text="分組"
android:id="@+id/button5"
android:layout_width="1dip"
android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
TableLayout 增加一個屬性 android:stretchColumns="*" 表示所有列都要自動展開,以便適應螢幕寬度。
它的值即可以是數字,也可以是*,注意數字是從0開始的,即:android:stretchColumns="1" 是設定 TableLayout所有行的第二列為擴充列。
上面我們會看到 第1列的按鈕比其他列的按鈕要寬,如果我們想都一樣寬如何辦呢?
一個簡單辦法:
android:layout_width="1dip"
摘自 奔跑的蝸牛