tablelayout布局内容居中的问题

本文探讨了在Android开发中,如何使用TableLayout实现表格内容居中,并解决最后一列在不同屏幕分辨率下自适应的问题。通过对布局参数的调整和适配策略,确保列表视图(ListView)在各种设备上展示效果良好。

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





用了android的各种布局,发现每种布局的居中,对齐方式都不一样,需要自己屡次尝试,甚是苦恼
给同样在苦恼中的人晒下TableLayout居中的方式
这是一个即将被我充斥到另外一个


<ListView android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/detail">
</ListView> 


里面去的Table,其实实现它的每单元格内容居中很简单,给它的上一级设置android:gravity="center"即可,还有个问题,需去掉下一级所有跟居中有关的属性
(之前没去掉,上级的属性都没生效了,奇怪?),代码如下:




<?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" >
    
         <TableRow android:gravity="center"> 
	         <TextView    
	               android:layout_height="wrap_content"    
	               android:layout_width="113dp"   
	               android:textSize="16dip"
	               android:textColor="@color/gray5"
	               android:id="@+id/clsname">  
	         </TextView>  
	         <TextView    
	               android:layout_height="wrap_content"   
	               android:layout_width="103dp"   
	               android:textSize="16dip"
	               android:textColor="@color/gray5"
	               android:id="@+id/salesamt">  
	         </TextView> 
	         <TextView    
	               android:layout_height="wrap_content"   
	               android:layout_width="match_parent"  
	               android:textSize="16dip"
	               android:textColor="@color/gray5"
	               android:id="@+id/profitamt">  
	         </TextView> 
         </TableRow> 
</TableLayout>





PS:最后一个TextViev里android:layout_width="match_parent"和父容器的android:layout_width="match_parent"搭配在一起为了

最后一列自适应屏幕分辨率



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值