【问题标题】:How do android:shrinkColumns and android:stretchColumns work?android:shrinkColumns 和 android:stretchColumns 是如何工作的?
【发布时间】:2015-11-26 16:13:05
【问题描述】:

我可以将android:shrinkColumnsandroid:stretchColumns 设置为TableLayout

例如:

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

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

【问题讨论】:

标签: android android-tablelayout stretch shrink


【解决方案1】:
  • android:stretchColumns

    要拉伸的列的从零开始的索引。列索引必须用逗号分隔:1、2、5。非法和重复的索引将被忽略。您可以改为使用值“*”来拉伸所有列。请注意,列可以同时标记为可拉伸和可收缩。

  • android:shrinkColumns

    要收缩的列的从零开始的索引。列索引必须用逗号分隔:1、2、5。非法和重复的索引将被忽略。您可以改用值“*”来缩小所有列。请注意,列可以同时标记为可拉伸和可收缩。

  • android:collapseColumns

    要折叠的列的从零开始的索引。列索引必须用逗号分隔:1、2、5。非法和重复的索引将被忽略。

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

解释:

android:stretchColumns="*"

意味着它根据表格布局宽度平均拉伸所有列

android:shrinkColumns="*"

意味着它缩小所有列

android:shrinkColumns="0,2"

android:stretchColumns="1"

表示第 0 列和第 2 列是包装,第 1 列延伸到可用宽度

android:stretchColumns="0,1,2"

android:shrinkColumns="1"

表示如果列已经拉伸然后收缩不适用

android:shrinkColumns="*"

android:collapseColumns="1"

android:collapseColumns 意味着它隐藏了给定的列

android:stretchColumns="*"

TextView :- android:layout_column="2"

表示如果表格行第一列布局参数不以0开头,则将空视图添加到行中

android:stretchColumns="*"

android:collapseColumns="1"

TextView :- android:layout_column="2"

表示如果 table-row 第一列布局参数不以 0 开头,则将空视图添加到行中,但如果折叠列则添加空视图,而不是隐藏该列索引仅通过显式视图隐藏添加的视图

我希望这会有所帮助。

【讨论】:

  • stretchColumns 和 shrinkColumns 是基于 0 的列。这意味着第一列是 0 而不是 1。
  • 谢谢您,非常好的解释
【解决方案2】:

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

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

表格的总宽度由其父容器定义。请务必记住,列可以可收缩和可拉伸

详情请访问

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

【讨论】:

  • 是的,这就是文档所说的 shrinkable 所做的。它似乎做的是导致没有列的空白行显示在表格中,这会浪费布局中的空间。我还没有看到它有什么帮助。另外,描述没有说明它缩小到什么,所以我不相信它。
猜你喜欢
  • 2018-11-10
  • 2018-02-12
  • 2018-06-21
  • 2012-09-13
  • 2014-03-29
  • 2013-09-06
  • 2011-01-03
  • 2012-03-12
  • 2010-09-26
相关资源
最近更新 更多