表格布局中android:collapseColumns和android:shrinkColumns的区别

4

我是Android开发的新手,正在阅读TableLayout,它有三个主要属性:

android:stretchColumns、android:collapseColumns和android:shrinkColumns。

经过一些研究,我明白了android:stretchColumns的含义,但是我对collapseColumns和shrinkColumns之间的区别感到困惑。官方文档如下:

android:shrinkColumns

指定需要收缩的列的从零开始的索引值。多个列的索引值用逗号分隔:1, 2, 5。如果存在非法或重复的索引值,将会被忽略。您可以使用"*"来表示所有列都需要收缩。请注意,一列可以同时被标记为可伸缩和可收缩。

android:collapseColumns

指定需要折叠的列的从零开始的索引值。多个列的索引值用逗号分隔:1, 2, 5。如果存在非法或重复的索引值,将会被忽略。

收缩(shrink)和折叠(collapse)的确切含义是什么呢?有人可以告诉我它们之间的区别吗?

2个回答

4
  • 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

意思是将表格中的所有列等比例地拉伸以适应表格布局的宽度

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

如果tablerow的第1列布局参数不以0开头,则会将空视图添加到行中

android:stretchColumns="*"

android:collapseColumns="1"

TextView:- android:layout_column="2"

如果表格行的第一列布局参数不以0开头,则会在行中添加空视图,但是如果您折叠了列,则添加的空视图不会隐藏该列索引,只有通过显式视图添加的视图才会被隐藏。

输入图像描述


2

缩小列

我们可以使用android:shrinkColumns属性来缩小或减小TableLayout中的一列或多列的宽度。在此属性中,我们可以指定单个列或逗号分隔的列号列表。指定的列中的内容会自动换行以减小它们的宽度。

折叠列

我们可以使用android:collapseColumns属性来使一列或多列列折叠或变得不可见。在此属性中,我们可以指定一个或多个逗号分隔的列。这些列是表信息的一部分,但是它们是不可见的。我们还可以通过将布尔值false和true传递给TableLayout中的setColumnCollapsed()方法来通过编码使列可见和不可见。


请给我一个例子,谢谢。 - Usman Anwer

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