android学习——TableLayout表格布局

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

android学习——TableLayout表格布局
 TableLayout表格布局
TableLayout是指将⼦元素的位置分配到⾏或列中。

Android的⼀个TableLayout有许多TableRow组成,每⼀个TableRow都会定义⼀个Row。

TableLayout容器不会显⽰Row,Column,及Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有⼀个View对象。

在使⽤tablelayout时,应注意每⼀个cell的宽度。

我们下⾯通过XML布局和Java代码布局两种⽅式分别举例:
⼀、XML⽅式布局
1、创建⼀个空⽩Activity
2、打开“res/layout/activity_main.xml”⽂件,修改成以下代码。

(1)第①部分
<?xml version="1.0" encoding="utf-8" ?>,每个XML⽂档都由XML序⾔开始,在前⾯的代码中的第⼀⾏便是XML序⾔,<?xml version="1.0">。

这⾏代码表⽰按照1.0版本的XML规则进⾏解析。

encoding = "utf-8"表⽰此xml⽂件采⽤utf-8的编码格式。

编码格式也可以是GB2312。

(2)第②部分
<LinearLayout …… 表⽰采⽤表格布局管理器。

(3)第③部分
android:layout_width="match_parent" android:layout_height="match_parent"表⽰布局管理器宽度和⾼充将填充整个屏幕宽度和⾼度。

(4)第④部分
android:stretchColumns="1"表⽰表格布局管理器中第2列内组件可以扩充到的有可⽤空间。

3、插⼊1⾏TableRow、1个⽂本TextView、1个TextEdit。

4、打开“res/layout/activity_main.xml”⽂件,修改成以下代码。

(1)第①部分
<TableRow></TableRow>代表⼀⾏,可以在其中填充控件。

(2)第②部分
添加⼀个标签<TextView>。

(3)第③部分
添加⼀个编辑框<EditText>。

5、依次再插⼊2⾏<TableRow>、密码标签<TextView>、密码编辑框<EditText>、2个按钮Button:注册、登录。

代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
//第⼀⾏
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="⽤户名:" />
<EditText
android:id="@+id/etUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</TableRow>
//第⼆⾏
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="登录密码:"
android:textStyle="bold"
android:gravity="right"
android:padding="3dp" />
<EditText
android:id="@+id/password"
android:password="true"
android:padding="3dp"
android:scrollHorizontally="true" />
</TableRow>
//第三⾏
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/cancel"
android:text="注册" />
<Button
android:id="@+id/login"
android:text="登录" />
</TableRow>
</TableLayout>
6、最终显⽰效果如下:
附:表格布局常见属性介绍
(1)TableLayout⾏列数的确定
TableLayout的⾏数由开发⼈员直接指定,即有多少个TableRow对象(或View控件),就有多少⾏。

TableLayout的列数等于含有最多⼦控件的TableRow的列数。

如第⼀TableRow含2个⼦控件,第⼆个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.
(2)TableLayout可设置的属性详解
TableLayout可设置的属性包括全局属性及单元格属性。

a)全局属性也即列属性,有以下3个参数:
android:stretchColumns 设置可伸展的列。

该列可以向⾏⽅向伸展,最多可占据⼀整⾏。

android:shrinkColumns 设置可收缩的列。

当该列⼦控件的内容太多,已经挤满所在⾏,那么该⼦控件的内容将往列⽅向显⽰。

android:collapseColumns 设置要隐藏的列。

⽰例:
android:stretchColumns="0" 第0列可伸展
android:shrinkColumns="1,2" 第1,2列皆可收缩
android:collapseColumns="*" 隐藏所有⾏
说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多⾏”显⽰其内容。

(这⾥不是真正
的多⾏,⽽是系统根据需要⾃动调节该⾏的layout_height)
b)单元格属性,有以下2个参数:
android:layout_column 指定该单元格在第⼏列显⽰
android:layout_span 指定该单元格占据的列数(未指定时,为1)
⽰例:
android:layout_column="1" 该控件显⽰在第1列
android:layout_span="2" 该控件占据2列
说明:⼀个控件也可以同时具备这两个特性。

⼆、Java代码⽅式布局
上⾯我们已经了解采⽤XML进⾏LinearLayout布局,我们现在再来学习⼀下如何使⽤Java代码完成与之同样功能。

Java代码⽅式暂略。

相关文档
最新文档