首先展示效果图
在xml添加如下代码
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,3"
tools:context=".MainActivity">
<TableRow>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账 号:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="邮箱或手机号"/>
<TextView/>
</TableRow>
<TableRow>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入6到16位数字或字母"/>
<TextView/>
</TableRow>
<TableRow>
<TextView/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注 册"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登 录"
android:background="#FF8247"/>
<TextView/>
</TableRow>
<TableRow
android:padding="20dp">
<TextView/>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:textColor="#FF4500"
android:gravity="right"/>
</TableRow>
</TableLayout>
Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。 当为View时,该View将独占一行。
二、TableLayout行列数的确定(最大的原则)
TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。TableLayout的列数等于含有最多子控件的TableRow的列数。如第一TableRow含2个子控件,第二个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.
三、TableLayout可设置的属性详解
总体了,这个TableLayout的属性和html中Table标签的属性差不多。他的各个属性如下:
TableLayout可设置的属性包括全局属性及单元格属性。
1、全局属性也即列属性,有以下3个参数:
android:stretchColumns 设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。
android:shrinkColumns 设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
android:collapseColumns 设置要隐藏的列。
示例:
android:stretchColumns="0" 第0列可伸展
android:shrinkColumns="1,2" 第1,2列皆可收缩
android:collapseColumns="*" 隐藏所有行
说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)
2、单元格属性,有以下2个参数:
android:layout_column 指定该单元格在第几列显示
android:layout_span 指定该单元格占据的列数(未指定时,为1)
示例:
android:layout_column="1" 该控件显示在第1列
android:layout_span="2" 该控件占据2列
说明:一个控件也可以同时具备这两个特性。