Android布局_表格布局TableLayout

一、TableLayout概述

  TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象

二、TableLayout的全局属性

   1、android:collapseColumns = "1,2"      

      隐藏从0开始索引列,列直接必须ongoing逗号隔开:1,2,5

<TableLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:collapseColumns="0" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个按钮"
            android:id="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个按钮"
            android:id="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三个按钮"
            android:id="@+id/button3" />
    </TableRow>
</TableLayout>

  2、android:shrikColumns = "1,2"      

      收缩从0开始的索引列。当可收缩的列太宽(内容过多)不会被挤出屏幕,列直接必须用逗号隔开:1,2,5,你可以通过"*"代替收缩所有列。注意一列能同时表示收缩和拉伸。

<TableLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="2" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个按钮"
            android:id="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个按钮"
            android:id="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三个按钮大神快放假了凯撒的解放路卡手机的路口发生的积分卡拉斯加"
            android:id="@+id/button3" />
    </TableRow>
</TableLayout>

  3、android:stretchColumns = "1,2"    

    拉伸从0开始的索引列。以填满剩下的多余空白控件,列直接必须用逗号隔开:1,2,3,可以通过"*"代替收缩所有列。注意一列能同时表示收缩和拉伸。

<TableLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="2" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个按钮"
            android:id="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个按钮"
            android:id="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三个按钮"
            android:id="@+id/button3" />
    </TableRow>
</TableLayout>

三、TableLayout的局部属性(内部控件所有属性)

  1、android:layout_column = "1"       该控件显示在第二列

   2、android:layout_span = "2"           该控件占据2列

四、实现简单的计算器布局

<TableLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:shrinkColumns="*">

    <TextView
        android:gravity="right|center"
        android:layout_width="fill_parent"
        android:layout_height="40"
        android:text="90"
        android:id="@+id/textView" />


    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:id="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:id="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:id="@+id/button3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:id="@+id/button4" />
    </TableRow>


    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:id="@+id/button5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:id="@+id/button6" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:id="@+id/button7" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:id="@+id/button8" />
    </TableRow>
</TableLayout>

  

原文地址:https://www.cnblogs.com/LO-ME/p/4700671.html