TableLayout 允许使用表格的方式来排列控件,这种布局也不常用,只需了解基本用法即可。
既然是表格,就一定会有行和列,再设计表格是应让每一行都有相同的列数,这样的表格也最简单。
当表格的某行一定要有不相等的列数时,就需要通过合并单元格的方式来应对。
设计一个登陆界面,允许用户输入账号密码后登陆
<TableLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<TableRow>
<TextView
android :layout_height= "wrap_content"
android :text= "Account" />
<EditText
android :id= "@+id/account"
android :layout_height= "wrap_content"
android :hint= "Input you account" />
</TableRow>
<TableRow>
<TextView
android :layout_height= "wrap_content"
android :text= "Passsword" />
<EditText
android :id= "@+id/password"
android :layout_height= "wrap_content"
android :hint= "textPassword" />
</TableRow>
<TableRow>
<Button
android :id= "@+id/login"
android :layout_width= "wrap_content"
android : layout_span = "2"
android :text= "Login" />
</TableRow>
</TableLayout>
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<TableRow>
<TextView
android :layout_height= "wrap_content"
android :text= "Account" />
<EditText
android :id= "@+id/account"
android :layout_height= "wrap_content"
android :hint= "Input you account" />
</TableRow>
<TableRow>
<TextView
android :layout_height= "wrap_content"
android :text= "Passsword" />
<EditText
android :id= "@+id/password"
android :layout_height= "wrap_content"
android :hint= "textPassword" />
</TableRow>
<TableRow>
<Button
android :id= "@+id/login"
android :layout_width= "wrap_content"
android : layout_span = "2"
android :text= "Login" />
</TableRow>
</TableLayout>
可以看出当前的登录界面并没有充分利用屏幕的宽度,右侧还空出了一块区域,因为在TableLayout中我们无法指定空间的宽度。
这时使用android:stretchColumns属性就可以很好地解决这个问题,它允许将TableLayout中的某一列进行拉伸,以达到自动适应屏幕宽度的作用。
<TableLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :stretchColumns= "1" >
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :stretchColumns= "1" >
android:stretchColumns="1" 将android:stretchColumns的值指定为1,表示如果表格不能完全占满屏幕宽度,就将第二列进行拉伸。
指定为“1”就是拉伸第二列,指定为“0”就是拉伸第一列。
<TableLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :stretchColumns= "0" >
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :stretchColumns= "0" >