您的位置:首页 > 移动开发 > Android开发

Android-布局管理-表格布局

2017-08-02 00:00 267 查看
摘要: 《Android从入门到精通》

简要概述:

表格布局与常见的彪哥类似,以行、列的形式来管理放入其中的UI组件。表格布局使用<TableLayout>标记定义,在表格布局中,可以添加多个<TableRow>标记,每个<TableRow>标记占用一行。由于<TableRow>标记也是容器,所有还可在该标记找那个添加其他组件,每没加一个组件,表格就会增加一列。在XML中表格布局的基本语法格式如下:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
属性列表>
<TableRow 属性列表>需要添加的UI组件</TableRow>
多个<TableRow>
</TableLayout>

TableLayout继承了LinearLayout,因此它完全支持LinearLayout所支持的全部XML属性。

LinearLayout相关介绍

同时还支持如下所示的XML属性

XML属性描述
android:collapseColumns设置需要被隐藏列的列序号(序号从0开始),多个列序号之间用逗号“,”分割。
android:shrinkColumns设置允许被收缩列的列序号(序号从0开始),多个列序号之间用逗号“,”分割。
android:stretchColumns设置允许被拉伸列的列序号(序号从0开始),多个列序号之间用逗号“,”分割。
android:layout_column该单元格在第几列显示
android:layout_span该单元格占据列数,默认为1

样例代码清单:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:stretchColumns="0,3">

<TableRow
android:id="@+id/tablerow1"
android:layout_weight="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView
android:id="@+id/textview1"
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="24px">
<EditText
android:id="@+id/edittext1"
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入帐号"
android:textSize="24px"
android:minWidth="400px"/>
<TextView />

</TableRow>
<TableRow
android:id="@+id/tablerow2"
android:layout_weight="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView
android:id="@+id/textview2"
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="密    码:"
android:textSize="24px"/>
<EditText
android:id="@+id/edittext2"
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textSize="24px"
android:minWidth="400px"/>
<TextView />

</TableRow>
<TableRow
android:id="@+id/tablerow3"
android:layout_weight="wrap_content"
android:layout_height="wrap_content">
<TextView />
<Button
android:id="@+id/button1"
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>

<Button
android:id="@+id/button2"
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
<TextView />

</TableRow>

</TableLayout>

效果图如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息