4、表格布局(TableLayout)
如果你接触过Web前端的东西的话,虽然常用的时div + css , 但是Web前端也是有表格布局的。在安卓开发中的表格布局和Web前端中的表格布局的概念类似,也就是通过画表表格的方式来实现布局。 在表格布局中,整个页面就相当于一张大的表格,控件就放在每个Cell中。接下来我们就使用表格布局来画一个表格,感受一下表格布局。接下来我们将会使用表格布局来实现一个比较经典的“登录”页面,下方是简单画的要实现效果图:
由上图我们容易看出,上面就是一个表格结构。Table中有3行两列,登录按钮占了一个整行,其余控件都占了一列。上面的布局还是蛮简单的,说白了,再复杂的布局也是从简单做起的。下方是实现上面布局的XML代码。
<TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns=""> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword"/> </TableRow> <TableRow> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="登录" android:layout_span=""/> </TableRow> </TableLayout>
其中android:stretchColumns="1"属性,表示让第一列(列数从零开始算起)拉伸,以达到视频屏幕的目的。所以你看到的输入框是充满后边整个屏幕的。登录按钮中这个属性android:layout_span="2" ,表明登录按钮跨两列。上述布局xml运行后的效果如下:











