android:shrinkColumns和android:stretchColumns是如何工作的?

8

我可以在TableLayout中设置android:shrinkColumnsandroid:stretchColumns

例如:

<TableLayout
    android:shrinkColumns="2,3"
    android:stretchColumns="1,3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

那么这些属性如何影响列呢?

1
我可以建议阅读Android开发者指南Android文档 - TableLayout的文档清楚地解释了它们的作用。 - Ed Holloway-George
2个回答

15
  • 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="1" />
    
        <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="2" />
    
        <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="3" />
    </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="1" />
    
        <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="2" />
    
        <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="3" />
    </TableRow>
    </TableLayout>
    

Explanations:

android:stretchColumns="*"

enter image description here

这意味着它会根据表格布局的宽度等比例地拉伸所有列

android:shrinkColumns="*"

enter image description here

这意味着它会缩小所有列。

android:shrinkColumns="0,2"

android:stretchColumns="1"

enter image description here

意味着第0列和第2列是包含内容的,第1列会根据可用宽度进行拉伸

android:stretchColumns="0,1,2"

android:shrinkColumns="1"

enter image description here

如果列已经被拉伸,则不适用收缩

android:shrinkColumns="*"

android:collapseColumns="1"

enter image description here

android:collapseColumns表示隐藏指定列

android:stretchColumns="*"

TextView:- android:layout_column="2"

enter image description here

如果表格行的第一列布局参数不以0开头,则会将空视图添加到该行。
android:stretchColumns="*"
android:collapseColumns="1"
TextView:- android:layout_column="2"

enter image description here

如果表格行的第一列布局参数不以0开始,则会向行中添加空视图,但如果您折叠列,则添加的空视图不会隐藏该列索引,只有显式视图才会隐藏添加的视图。
我希望这能帮到你。

stretchColumns和shrinkColumns是基于0的列。这意味着第一列是0而不是1。 - Simon
谢谢您,讲解得非常好。 - FireTr3e
为什么需要使用android:layout_column="0"等属性,因为它们会自动分配。 - Payel Senapati
@PayelSenapati 很好的问题..我为每个子项编写其位置,因为在阅读或编码时很容易理解我正在处理哪一列...当我有超过15-20或50列时,如何在向上/向下滚动xml时知道我当前关注的是哪一列?描述每个列与其位置很容易,因此在滚动或执行某些操作时不必混淆或计算每个列...我只需从子视图中读取列索引,这对我来说很容易。 - BiRjU

7
TableLayout 可以通过调用 setColumnShrinkable()(xml:android:shrinkColumns) 或 setColumnStretchable()(xml:android:stretchColumns) 指定某些列可收缩或可伸展。若标记为可收缩,则该列的宽度可以缩小以适应表格的父对象;若标记为可伸展,则其宽度可以扩展以适应任何额外空间。表格的总宽度由其父容器定义。需要记住的是,一列既可以被标记为可收缩也可以被标记为可伸展。更多详细信息请参见:

https://developer.android.com/reference/android/widget/TableLayout.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接