Android的TableLayout布局的介绍

TableLayout布局是网格布局。

它拥有<TableRow>标记代表一行,它在布局里面相当于一个小容器。网格布局没有代表列的标记,列的实现是通过在行标记里面添加组件,每个组件占一列。注意:每个行的高度是由此行中高度值最大的组件决定,每一列的长度由这一列中宽度值最大的组件决定,还要注意在一行中高度小于行高度的组件,组件实际的高度不变,只是组件位于小网格的中间,而列的宽度决定了组件实际显示的大小,即使组件没有这么宽,也会被放大。<TableRow>标记有一个android:padding属性,用于设置此行的内边距。

布局属性android:collapseColumns:取值为0,1..它代表隐藏相应序号的列,后面的列向前补齐,如果为多个值,值间用逗号隔开。

布局属性android:stretchColumns:取值0,1..它代表允许被拉伸的列,被拉伸的列平均分配剩余行中未沾满的空间,注意设置能够拉伸的组件的android:gravity属性让组件内容居中,这样拉伸后才美观。这个属性+空白列(实际存在,占用列序号,只是看不到空间大小)可以用于将主要组件在某些位置居中显示适应不同屏幕分辨率需求,让空白列增加或减少以使空白列均分行中未使用空间,这样就使得主要的控件位于中间。

布局属性android:shrinkColumns:取值0,1..他代表允许被收缩的列。每行中空间足够时按原大小显示,当一行中空间不足时,超过屏幕的部分是不会被显示的,只有设置了某一列或多列为可收缩(其实是增加高度来实现内容的显示)时,屏幕能够显示全部内容。如果设置了一些列为可收缩且此时空间已满,再增加组件,则可收缩组件各自收缩一部分(都会收缩,至于占比,这不是这篇文章的重点,需要注意的是可能组件收缩后显示不了全部内容)。

注意:如果不使用<TableRow>标记直接增加组件,那么组件将独占一行。测试了一下在组件中设置宽度为match_parent属性和wrap_content初始显示时没有任何区别,但如果是EditText组件被设置为match_parent值那么它的大小一旦改变则不会还原占据屏幕剩余空间,设置为wrap_content则一直为包裹内容大小(包括隐藏文本),通过这个例子说明虽然起始显示无差别,但是系统内部设置是有区别的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义 Android TableLayout 的样式,可以使用以下步骤: 1. 创建一个新的 xml 文件,例如 custom_table.xml。 2. 在该文件中定义 TableLayout 的属性,例如背景颜色、边框等等。可以参考 TableLayout 的属性列表来定义。 3. 在该文件中定义 TableRow 的属性,例如背景颜色、边框等等。可以参考 TableRow 的属性列表来定义。 4. 在该文件中定义 TextView 的属性,例如字体颜色、大小、对齐方式等等。可以参考 TextView 的属性列表来定义。 5. 将 TableLayout、TableRow 和 TextView 放置在合适的位置,并按照需要对其进行布局。 以下是一个示例 custom_table.xml 文件: ```xml <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:padding="10dp" android:stretchColumns="*"> <TableRow android:background="#e0e0e0" android:layout_height="wrap_content" android:layout_width="match_parent" android:padding="5dp"> <TextView android:text="Name" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="Age" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="Gender" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </TableRow> <TableRow android:background="#f0f0f0" android:layout_height="wrap_content" android:layout_width="match_parent" android:padding="5dp"> <TextView android:text="John Doe" android:textColor="#000000" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="25" android:textColor="#000000" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="Male" android:textColor="#000000" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </TableRow> <TableRow android:background="#f0f0f0" android:layout_height="wrap_content" android:layout_width="match_parent" android:padding="5dp"> <TextView android:text="Jane Doe" android:textColor="#000000" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="30" android:textColor="#000000" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="Female" android:textColor="#000000" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </TableRow> </TableLayout> ``` 在代码中使用该自定义 TableLayout 的示例: ```java // 加载自定义 TableLayout布局 LayoutInflater inflater = LayoutInflater.from(this); View customTable = inflater.inflate(R.layout.custom_table, null); // 将自定义 TableLayout 添加到主布局中 LinearLayout mainLayout = findViewById(R.id.main_layout); mainLayout.addView(customTable); ``` 以上是自定义 Android TableLayout 样式的基本步骤,可以根据需要进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值