2-5 基本布局–TableLayout 表格布局
概述:表格布局是以行数和列数来确定位置进行排列。就像一间教室,确定好行数与列数就能让同学有序入座。
注意:我们需要先添加<TableRow
容器,每添加一个就会多一行,然后再往<TableRow
容器中添加其它组件。
TableLayout 常用布局标签:
<android:collapseColumns="1"
:表示隐藏第二列
<android:stretchColumns="0"
:表示将第1列设置为扩展列,若有空余空间,则会被设定的第1列占满。可以同时设置多列平分空间。
<android:layout_margin="1dip"
:可以设置组件与父件之间的间隔距离,为1dip
TableLayout 单元格常用布局标签:
<android:layout_span="2"
:合并单元格,从当前行向右。
<android:layout_margin="0.5dip"
:可以设置单元格之间的间隔距离,为0.5dip。两个单元格都设置了0.5dip,所以实际上单元格之间的距离为1dip。
代码示例
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:stretchColumns="0,1,2,3"
android:orientation="horizontal">
<TableRow
android:background="#ffffcc99"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0.5dip">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:gravity="center"/>
<TextView
android:id="@+id/textView