TableLayout和TableRow注意事项

本文介绍了TableLayout在Android开发中的使用注意事项,包括如何防止控件超出屏幕宽度、控件排列方式以及TableRow中控件的对齐规则。通过示例说明了在TableRow中,每个控件会与所在列中最宽的控件对齐,并确保同一行内的控件对齐。

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

1.为了防止TableLayout里面的控件超出显示屏幕的宽度
加上下面这句话,使得它可以伸缩

android:shrinkColumns="1"

2.TableLayout(不用TableRow)每一个控件成为一行,用TableRow,一个TableRow里面所有的控件自成一行
3.在使用TableRow的时候需要注意,每一个TableRow的每一个控件都和所在的列里面最长width最大的那个对齐,并且每一行的每一个控件都相互对齐
比如:

<?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:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.administrator.radiogrouppractice.MainActivity">

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:shrinkColumns="1">
        <!--第一行“姓名+编辑框”-->
        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名:"
                android:textColor="#000000"
                android:textSize="20dp"/>
            <EditText
                android:id="@+id/edt"
                android:layout_height="wrap_content"
                android:layout_width="200dp"
                />
        </TableRow>
        <!--第二行 “血型”+RadioGroup-->
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="血型:"
                android:textSize="20dp"
                android:textColor="#000000"/>
            <RadioGroup
                android:id="@+id/RG"
                android:orientation="vertical">
                <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O型"
                    android:textSize="20dp"/>
                <RadioButton
                    android:id="@+id/rb2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="A型"
                    android:textSize="20dp"/>
                <RadioButton
                    android:id="@+id/rb3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="B型"
                    android:textSize="20dp"/>
                <RadioButton
                    android:id="@+id/rb4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="AB型"
                    android:textSize="20dp"/>
            </RadioGroup>
        </TableRow>
        <TextView
            android:id="@+id/tv"
            android:textSize="20dp"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>
    </TableLayout>
</RelativeLayout>

这里写图片描述

如果下面加3个按钮就变成:
这里写图片描述
如果下面只有一个按钮,就算你设置成fill_parent也不会铺满整个TableRow。
这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值