Android中的表格布局

本文介绍了Android中TableLayout的使用,包括重要属性android:stretchColumns、android:shrinkColumns和android:collapseColumns的详细解释,并通过一个计算器布局的示例展示了如何创建和调整表格布局,以实现屏幕适配和功能布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 Android中的表格布局

重要属性:

  • android:stretchColumns
  • android:shrinkColumns
  • android:collapseColumns

效果如下:
在这里插入图片描述
xml文件如下:

<?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:stretchColumns="*">
    <!--如果直接往TableLayout中添加控件,那么该控件与屏幕等宽
        如果向使多个控件在同一行,那么我们在这些控件外层包裹一对TableRow
        并且在这种情况下,控件宽度与内容适配
        android:stretchColumns="*"    设置可以伸展的列,直接传列的索引,如果有多列,
        以,作为分割,*表示全部
        android:shrinkColumns    设置可以缩小的列
        android:collapseColumns="1"   设置可隐藏的列
    -->
    <EditText android:layout_weight="2"
        android:text="0"
        android:gravity="right|center_vertical"
        android:textSize="28sp"
        />
    <TableRow android:layout_weight="1">

        <Button
            android:layout_height="match_parent"
            android:text="C"
            android:textSize="24sp" />
        <Button
            android:layout_height="match_parent"
            android:text="+/-"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="%"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="÷"
            android:textSize="24sp"/>
    </TableRow>
    <TableRow android:layout_weight="1">
        <Button
            android:layout_height="match_parent"
            android:text="7"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="8"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="9"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="*"
            android:textSize="24sp"/>
    </TableRow>
    <TableRow android:layout_weight="1">
        <Button
            android:layout_height="match_parent"
            android:text="4"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="5"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="6"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="-"
            android:textSize="24sp"/>
    </TableRow>
    <TableRow android:layout_weight="1">
        <Button
            android:layout_height="match_parent"
            android:text="1"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="2"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="3"
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="+"
            android:textSize="24sp"/>
    </TableRow>
    <TableRow android:layout_weight="1">
        <Button
            android:layout_height="match_parent"
            android:text="0"
            android:textSize="24sp"
            android:layout_span="2"/>
        <Button
            android:layout_height="match_parent"
            android:text="."
            android:textSize="24sp"/>
        <Button
            android:layout_height="match_parent"
            android:text="="
            android:textSize="24sp"/>
    </TableRow>
</TableLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值