先來上個(gè)圖,最終效果圖。
http://wiki.jikexueyuan.com/project/android-actual-combat-skills/images/34-1.png" alt="fig.1" />
每個(gè) Layout 都有自己最適用的場景,而 TableLayout 往往用在中規(guī)中矩的輸入界面,比如下圖:
http://wiki.jikexueyuan.com/project/android-actual-combat-skills/images/34-2.png" alt="fig.2" />
TableLayout 下嵌套 TableRow 組成 Table 的行;每個(gè) TableRow 中布局不同的控件,組成 Table 的列。上圖就是兩列四行,而我們要做的表格就是三行三列。
TableLayout 是沒有提供邊框的,要作成表格的效果需要我們使用一些技巧:
整個(gè) TableLayout 的背景色設(shè)成黑色,而每個(gè) Table 的 cell(其實(shí)就是每個(gè)控件)的背景色設(shè)成白色,然后 cell 的邊距根據(jù)情況設(shè)置一個(gè) px,就可以了。
布局代碼如下:
<TableLayout
android:id="@+id/table"
android:layout_below="@id/spinner"
android:layout_width="wrap_content"
android:background="@color/black"
android:layout_height="wrap_content">
<TableRow
android:layout_marginTop="1dp"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="姓名"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_width="@dimen/table_item1_width"
android:layout_height="wrap_content" />
<TextView android:text="學(xué)校"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_marginLeft="1dp"
android:layout_width="@dimen/table_item2_width"
android:layout_height="wrap_content" />
<TextView android:text="專業(yè)"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_marginLeft="1dp"
android:layout_width="@dimen/table_item3_width"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="張飛"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_width="@dimen/table_item1_width"
android:layout_height="wrap_content" />
<TextView android:text="野雞大學(xué)"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_marginLeft="1dp"
android:layout_width="@dimen/table_item2_width"
android:layout_height="wrap_content" />
<TextView android:text="體育系"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_marginLeft="1dp"
android:layout_width="@dimen/table_item3_width"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_marginBottom="1dp"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="劉備"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_width="@dimen/table_item1_width"
android:layout_height="wrap_content" />
<TextView android:text="皇家學(xué)院"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_marginLeft="1dp"
android:layout_width="@dimen/table_item2_width"
android:layout_height="wrap_content" />
<TextView android:text="經(jīng)濟(jì)"
android:textSize="@dimen/table_text_size"
android:background="@color/white"
android:layout_marginLeft="1dp"
android:layout_width="@dimen/table_item3_width"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>