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

问题描述 投票:6回答:2

我可以在android:shrinkColumns设置android:stretchColumnsTableLayout

例如:

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

那么这个属性如何影响列?

android android-tablelayout stretch shrink
2个回答
6
投票

TableLayout可以通过调用setColumnShrinkable()(xml:android:shrinkColumns) or setColumnStretchable()(xml:android:stretchColumns)将某些列指定为可缩小或可伸缩的。

如果标记为可收缩,则可以缩小列宽以使表适合其父对象。如果标记为可拉伸,则可以扩展宽度以适应任何额外空间。

表的总宽度由其父容器定义。重要的是要记住,柱子既可收缩又可伸缩。

有关详细信息,您可以访问

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


7
投票

也许这可以帮助

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

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:background="@color/red"
            android:text="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:background="@color/green"
            android:text="2" />

        <TextView
            android:layout_width="wrap_content"
            android:background="@color/blue"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="3" />

    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:background="@color/red"

            android:text="4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:background="@color/green"

            android:text="5" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:background="@color/blue"
            android:text="6" />
    </TableRow>
</TableLayout>

说明:

机器人:shrinkColumns = “2,3”

机器人:stretchColumns = “1”

意味着第2列和第3列将缩小,第1列将被拉伸enter image description here

机器人:shrinkColumns = “3”

机器人:stretchColumns = “1,2”

意味着柱3将收缩,并且柱1和2将被拉伸

enter image description here

机器人:shrinkColumns = “1”

机器人:stretchColumns = “2,3”

意味着柱3将收缩,并且柱1和2将被拉伸

enter image description here

android:stretchColumns =“*”表示所有列都将被均匀拉伸

enter image description here

android:shrinkColumns =“*”表示所有列均匀缩小

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.