当前位置:操作系统 > 安卓/Android >>

Android应用开发之TableLayout (表格布局)+信息列表案例

 

表格布局的风格跟HTML 中的表格比较接近,只是所采用的标签不同。

□<TableLayout > 是顶级元素,采用的是表格布局

□<TableRow> 定义一个行

□<TextView > 定义一个单元格的内容

示例main.xml布局文件内容如下:

 

<? xml version = "1.0"encoding = "utf-8" ?> 

< TableLayout 

xmlns:android = "http://schemas.android.com/apk/res/android 

android:layout_width ="fill_parent" 

android:layout_height ="fill_parent" 

android:stretchColumns ="0,1,2,3" 

>  

< TableRow > 

< TextView 

android:text = "@string/name" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text = "@string/gender" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text = "@string/age" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text = "@string/phonenum" 

android:gravity = "center" 

android:padding = "3dip" /> 

</ TableRow > 

< TableRow > 

< TextView 

android:text = "@string/name1" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text = "@string/gender1" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text = "@string/age1" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text ="@string/phonenum1" 

android:gravity = "center" 

android:padding = "3dip" /> 

</ TableRow > 

< TableRow > 

< TextView 

android:text = "@string/name2" 

android:gravity = "center" 

android:padding = "3dip" />  

< TextView 

android:text = "@string/gender1" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text = "@string/age2" 

android:gravity = "center" 

android:padding = "3dip" /> 

< TextView 

android:text ="@string/phonenum2" 

android:gravity = "center" 

android:padding = "3dip" /> 

</ TableRow > 

</ TableLayout > 

□android:stretchColumns="0,1,2,3"

该属性指定每行都由第“0 、1 、2 、3 ”列占满空白空间。

□gravity 指定文字对齐方式,案例都设为居中对齐。

□padding 指定视图与视图内容间的空隙,单位为像素。

对应的strings.xml 文件内容如下:

 

<? xml version = "1.0"encoding = "utf-8" ?> 

< resources > 

< string name = "name" > 姓名</string > 

< string name = "gender" > 性别</string > 

< string name = "age" > 年龄</string > 

< string name = "phonenum"> 电话</ string > 

< string name = "gender1" >男</ string > 

< string name = "gender2" >女</ string > 

< string name = "name1" > 张三</string > 

< string name = "age1" > 25</ string > 

< string name = "phonenum1"> 1234567 </ string > 

< string name = "name2" > 李四</string > 

< string name = "age2" > 24</ string > 

< string name = "phonenum2"> 7654321 </ string > 

</ resources > 

界面效果如下:

\

 

摘自 潇洒哥的专栏

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,