TableLayout表格布局UI

本文介绍如何在Android项目中使用TableLayout来并排显示TextView,并通过添加padding属性解决文字挤在一起的问题。

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

新建一个android项目时,UI的默认布局是LinearLayout(线性排版),如果要要实现并排显示TextView,我们可以使用TableLayout(表格排版)。

android.widget.TableLayout 是一個“排版类别”,它可以将画面切割成一个表格,下面在上个工程中添加一个两列表格的例子。

编辑main.XML文件:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <WebView 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:id="@+id/wv" 
	/>
    <TableRow>
		<TextView  
		    android:layout_width="fill_parent" 
		    android:layout_height="wrap_content" 
		    android:text="www.baidu.com"
		    android:padding="5dip"
		    android:autoLink="web" />
		<TextView  
		    android:layout_width="fill_parent" 
		    android:layout_height="wrap_content" 
		    android:text="www.google.com"
		    android:autoLink="web" />
	</TableRow>
    

</TableLayout>

android.widget.TableRow配合TableRow使用的一个类,当在TableLayout添加一个TableRow时,在TableRow里面所有的View都会在同一个列(row)中显示。

为了避免同一行所有的文字都挤在一起,因此在TextView中加上padding的属性:

 android:padding="5dip"
表示第一个TextView的padding(间格)为5dip,表示与旁边的View有5个空格,这样就解决了两个TextView不会挤在一起。
效果如图:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值