Android TableLayout的使用

来源:互联网 发布:同花顺软件乱码 编辑:程序博客网 时间:2024/03/29 18:43

辛苦堆砌,转载请注明出处,谢谢!

之前做表单类型的界面,一直使用的是LinearLayout配合layout_weight的方式,今天无意中看到了TableLayout,发现做表单甚是好用,自己默守陈规,孤陋寡闻,不过在Android API文档的布局一栏下,也没有看到google放置该布局的说明,原因不去考究了咱们只管用用,今天发文记录一下。


TableLayout用来将子视图按照行列的方式进行排布,其子视图可以是TableRow,也可以是单独的一个View类,此时该View类的控件独自占用一行。TableLayout的子视图不需要指定layout_width,其默认为match_parent,可以指定layout_height,不过其默认为wrap_content,所以一般也不需要指定。不过这里注意,由于TableLayout的layout_width为match_parent,我们TableRow下的View可以使用layout_weight的方式调整一行中各个单元格的宽度。


影响TableLayout布局的重要属性主要有三个,这三个属性值为逗号分隔的列索引值,索引值从0开始,也就是第一列索引为0,第二列索引为1,以此类推,用来控制列的行为:

android:collapseColumns:指定折叠起来的列,使用该属性指定的列会被折叠,目前我还没有用到它的时候,将来慢慢体会。

android:shrinkColumns:指定的列宽将被压缩为该列中最长的单元格的宽度

android:stretchColumns:指定的列宽会自动扩展,以占用该行剩余的空间


另外,我们可以将android:layout_span属性添加到一个View类上,以指明该控件占据的列数。


大概就是这些内容,下面给出一个列子,可能不太合适,但是只是为了演示TableLayout的使用,也基本足够了

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="10dp"    tools:context="com.yjp.tablelayoutdemo.MainActivity">    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:stretchColumns="1">        <TableRow>            <TextView android:text="姓名" />            <EditText                android:layout_span="2"                android:hint="请输入姓名"/>        </TableRow>        <TableRow>            <TextView android:text="年龄"/>            <EditText                android:layout_span="2"                android:hint="请输入年龄"/>        </TableRow>        <TableRow>            <TextView android:text="身份证" />            <EditText android:hint="请输入身份证号" />            <Button                android:layout_width="40dp"                android:layout_height="40dp"                android:background="@android:drawable/ic_menu_camera"/>        </TableRow>        <TableRow>            <TextView                android:layout_gravity="center"                android:text="户口类型"/>            <RadioGroup                android:orientation="horizontal"                android:layout_span="2"                android:layout_gravity="center">                <RadioButton                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="非农业户口"/>                <RadioButton                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="农业户口"/>            </RadioGroup>        </TableRow>    </TableLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="提交"/></RelativeLayout>
界面如图:


可以看到,我们做表单省掉了很多的layout_width和layout_height,但是控件的分配还是比较协调的。当我们的表单比较大的时候,使用TableLayout可以很好地完成工作。



1 0