Android TableLayout 表格布局详解

TableLayout: 表格布局

一 里面的元素,默认占一行


二 TableLayout 一般和 TableRow 结合在一起用
 TableRow里的内容超出一行的宽度,不会自动换行, 会显示不全


三 常见属性


 1 写在父元素上
    collapseColumns : 隐藏第几列的子元素(从0开始数的)
    stretchColumns :拉伸第几列的子元素(从0开始数的),有剩余空间才能拉伸
    shrinkColumns:收缩第几列的子元素(从0开始数的),当这一行的显示超出了,有部分元素显示不全的时候才有效


 2 写在子元素上
     layout_column : 设置在第几列显示,该元素后面的元素也会跟着变动
     layout_span :  横向跨几列
 

一 里面的元素,默认占一行

代码

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

    <Button
        android:text="hello"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <Button
        android:text="girl"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</TableLayout>

效果

二 TableLayout 一般和 TableRow 结合在一起用

TableRow里的内容超出一行的宽度,不会自动换行, 会显示不全

代码

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

    <TableRow>
    <Button
        android:text="hello"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <Button
        android:text="girl"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</TableRow>
    <TableRow>
        <Button
            android:text="hello"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="girl"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="hello"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="girl"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="girl"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

    </TableRow>
<Button
    android:text="hello"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
<Button
    android:text="girl"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

</TableLayout>

 三 常见属性

 1 写在父元素上
    collapseColumns : 隐藏第几列的子元素(从0开始数的)
    stretchColumns :拉伸第几列的子元素(从0开始数的),有剩余空间才能拉伸
    shrinkColumns:收缩第几列的子元素(从0开始数的),当这一行的显示超出了,有部分元素显示不全的时候才有效
 2 写在子元素上
     layout_column : 设置在第几列显示,该元素后面的元素也会跟着变动
     layout_span :  横向跨几列

代码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="0"
    android:stretchColumns="1"
    android:shrinkColumns="2,5"
xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow>
    <Button
        android:text="hello"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <Button
        android:text="girl"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</TableRow>
    <TableRow>
        <Button
            android:text="第1个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="第2个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="第3个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="第4个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="第5个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="第6个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="第7个"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

    </TableRow>
    <TableRow>
        <Button
            android:text="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button
            android:text="2"
            android:layout_column="4"
            android:layout_span="3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <Button

            android:text="3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>


    </TableRow>
<Button
    android:text="hello"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
<Button
    android:text="girl"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

</TableLayout>

 效果

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值