安卓(TableLayout)

摘要:
单元格可以跨列,就像在HTML中一样。然而,TableLayout可以通过调用setColumnShrinkable()或由其父容器定义,将某些列指定为可收缩或可拉伸。TableLayout的子级不能指定布局宽度属性。如果跳过列编号,则列编号将被视为行中的空单元格。有关使用XML创建表的示例,请参阅ApiDemos中的TableLayout示例。虽然TableLayoutis的典型子类是tablerow,但实际上,您可以使用任何视图子类作为TableLayout的直接子类。视图将显示为所有表列中的单行。

1、特点

公共类TableLayout扩展LinearLayout容器不显示其行、列或单元格的边框线。每一行有零个或多个单元格;每个单元格可以包含一个视图对象该表的列数与包含最多单元格的行的列数相同。一个表可以保留空单元格。单元格可以跨列,就像在HTML中一样。

列的宽度由该列中单元格最宽的行定义。但是,TableLayout可以通过调用setColumnShrinkable()或由其父容器定义,将某些列指定为可收缩或可拉伸的。重要的是要记住,既可以收缩,也可以拉伸。在这种情况下,列将更改其大小,以始终使用可用空间,但永远不会超过。最后,可以通过调用setcolumnclapsedo隐藏列。

TableLayout的子级不能指定布局宽度属性。宽度始终匹配。但是,布局右心房可以由子节点定义;默认值为ViewGroup。包装内容。如果孩子是一个表格行,那么高度总是ViewGroup。布局巴拉那。包装内容。

在代码和XML中,单元格必须按列的递增顺序添加到行中。列号是基于零的。如果不为子cel指定列号,它将自动递增到下一个可用列。如果跳过列号,则该列号将被视为该行中的空单元格。有关使用XML创建表的示例,请参阅ApiDemos中的TableLayout示例。

尽管TableLayoutis的典型子类是table row,但实际上您可以将任何视图子类用作TableLayout的直接子类该视图将显示为跨所有表列的单行。

(1)有一系列的tableRow对象组成,不显示边框线,可以标记为扩展或拉伸,总的宽度由父容器定义

(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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:stretchColumns="0"
    >

<TableRow>
    <Button android:text="button1"/>
    <Button android:text="button2"/>
    <Button android:text="button3"/>
</TableRow>


</TableLayout>

安卓(TableLayout)第1张

 第一列拉伸,第二列收缩:

<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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:stretchColumns="0"
    android:shrinkColumns="1">

<TableRow>
    <Button android:text="button1"/>
    <Button android:text="button2"/>
    <Button android:text="button3"/>
</TableRow>


</TableLayout>

安卓(TableLayout)第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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:collapseColumns="1"
    >

<TableRow>
    <Button android:text="button1"/>
    <Button android:text="button2"/>
    <Button android:text="button3"/>
</TableRow>


</TableLayout>

安卓(TableLayout)第3张

(4)子组件不能设置layout_width属性,因为它的宽度不能由自己来决定,可以定以高度

(5)列号是从零开始的

(6)可以用某一个View组件直接作为表格的子组件,则该组件独占一行

<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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 <Button android:text="button3"/>
</TableLayout>

安卓(TableLayout)第4张

2、TableLayout实现计算器界面

(1)核心代码:

<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="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    
    
    <TextView
        android:layout_height="160sp"
        android:text="12345"/>    

    <TableRow>
            <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mc"/>
             <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="m+"/>
              <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="m-"/>
               <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mr"/>
    </TableRow>
    
        <TableRow>
            <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C"/>
             <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"/>
              <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"/>
               <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"/>
    </TableRow>
    
            <TableRow>
            <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"/>
             <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"/>
              <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"/>
               <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"/>
    </TableRow>
    
            <TableRow>
            <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"/>
             <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"/>
              <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"/>
               <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"/>
    </TableRow>
    
            <TableRow>
            <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"/>
             <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"/>
              <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"/>
                  <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""/>
    </TableRow>
    
                    <TableRow>
            <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="%"/>
             <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"/>
              <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="."/>
               <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="="/>
    </TableRow>
</TableLayout>

(2)显示效果:

安卓(TableLayout)第5张

免责声明:文章转载自《安卓(TableLayout)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux Boot目录满了之后的解决方法LaTex 编辑器知多少下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Scala入门系列(十一):模式匹配

引言 模式匹配是Scala中非常有特色,非常强大的一种功能。 类似于Java中的switch case语法,但是模式匹配的功能要比它强大得多,switch只能对值进行匹配,但是Scala的模式匹配除了可以对值进行匹配之外,还可以对类型进行匹配、对Array和List的元素情况进行匹配、对case class进行匹配甚至对有值或没值(Option)进行匹配...

前端重点题目汇总

1 . Blob 对象中的type属性表示的文件是 MIME 类型。   Blob构造函数返回一个新的Blob对象,基本语法是 var aBlob = new Blob( array, options );   其中 array 是 ArrayBuffer, ArrayBufferView, Blob, DOMString 等对象构成的 Array ,或者...

导出Excel之Epplus使用教程2(样式设置)

 导出Excel之Epplus使用教程1(基本介绍)  导出Excel之Epplus使用教程2(样式设置)   导出Excel之Epplus使用教程3(图表设置)    导出Excel之Epplus使用教程4(其他设置) 1、公式计算      excel中离不开各种各样的公式计算,在Epplus中运用公式有两种方式,你都可以尝试一下: 1 2...

微信小程序学习之for循环

一、使用自定义创建的json数据 1. 创建微信小程序项目后 在wxml文件中增加for循环 <block wx:for="{{posts_key}}" wx:for-item="item" wx:for-index="idx"> <view class="post-container"> <view...

获取apk的Activity和Package

2.查看包名和activity adb logcat|grep -i activitymanager 获取当前界面的activity 1.adb shell dumpsys window windows 2.adb shell dumpsys window windows| grep mCurrentFocus 3.获取Package aapt d...

超简单,安卓模拟器手动root

本文转载自:http://quantoubao.blog.163.com/blog/static/2083211702013870501987/ 安装Android SDK安卓模拟器的方法很简单,网上大把,傻瓜式的。不过对其root的方法,网上的版本就不那么好使了。网上的方法从方向性来说是没错的,就是细节没给讲清楚,或者讲错,或者没提到这样root方式对高...