android:stretchColumns
The zero-based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5. Illegal and duplicate indices are ignored. You can stretch all columns by using the value "*" instead. Note that a column can be marked stretchable and shrinkable at the same time.
android:shrinkColumns
The zero-based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 5. Illegal and duplicate indices are ignored. You can shrink all columns by using the value "*" instead. Note that a column can be marked stretchable and shrinkable at the same time.
android:collapseColumns
The zero-based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5. Illegal and duplicate indices are ignored.
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="@color/grey">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:background="@color/red"
android:textColor="@android:color/white"
android:textSize="30dp"
android:text="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:textColor="@android:color/white"
android:textSize="30dp"
android:background="@color/green"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:textColor="@android:color/white"
android:textSize="30dp"
android:background="@color/blue"
android:text="2" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:background="@color/red"
android:textColor="@android:color/white"
android:textSize="30dp"
android:text="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:textColor="@android:color/white"
android:textSize="30dp"
android:background="@color/green"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:textColor="@android:color/white"
android:textSize="30dp"
android:background="@color/blue"
android:text="2" />
</TableRow>
</TableLayout>
解释:
android:stretchColumns="*"
![enter image description here](https://istack.dev59.com/TkbsR.webp)
意思是将表格中的所有列等比例地拉伸以适应表格布局的宽度
android:shrinkColumns="*"
![enter image description here](https://istack.dev59.com/mQIZ2.webp)
意思是缩小所有列
android:shrinkColumns="0,2"
android:stretchColumns="1"
![enter image description here](https://istack.dev59.com/jBCoe.webp)
意思是第0和第2列包含内容而第1列被拉伸以适应可用宽度
android:stretchColumns="0,1,2"
android:shrinkColumns="1"
![enter image description here](https://istack.dev59.com/TkbsR.webp)
意思是如果列已经被拉伸,则不应用缩小操作
android:shrinkColumns="*"
android:collapseColumns="1"
![enter image description here](https://istack.dev59.com/9iJ2s.webp)
android:collapseColumns表示隐藏指定的列
android:stretchColumns="*"
TextView:- android:layout_column="2"
![enter image description here](https://istack.dev59.com/55qoS.webp)
如果tablerow的第1列布局参数不以0开头,则会将空视图添加到行中
android:stretchColumns="*"
android:collapseColumns="1"
TextView:- android:layout_column="2"
如果表格行的第一列布局参数不以0开头,则会在行中添加空视图,但是如果您折叠了列,则添加的空视图不会隐藏该列索引,只有通过显式视图添加的视图才会被隐藏。
![输入图像描述](https://istack.dev59.com/7xASu.webp)