通过findViewById获取TableLayout中的一组TableRow

Get a set of TableRow in TableLayout by findViewById

有LinearLayout,其中某些部分可能在某些情况下不可见。 例如

<LinearLayout>
  <LinearLayout android:id="@+id/layout1">
     <!-- set of childs -->
  </LinearLayout>
  <LinearLayout android:id="@+id/layout2">
     <!-- set of childs -->
  </LinearLayout>
</LinearLayout>

在 LinearLayout 中,我可以通过 findViewById 从主布局中获取任何子部分(及其布局)并更改它的可见性(当然是通过编程方式)。带有 ids 的布局像包装器一样用于子集。

我可以在 TableLayout 中做这样的事情吗?我的意思是,通过 findViewById 从布局中获取一组 TableRow 并更改他的可见性?

TableRow继承自LinearLayout。它可以做线性布局可以做的一切。所以是的,您可以为其分配一个 ID 并更改其可见性。

http://developer.android.com/reference/android/widget/TableRow.html

来自文档 http://developer.android.com/reference/android/widget/TableLayout.html:

Although the typical child of a TableLayout is a TableRow, you can actually use any View subclass as a direct child of TableLayout. The View will be displayed as a single row that spans all the table columns.

所以,我这样做

<TableLayout>
  <TableRow><!-- some data --></TableRow>
  <TableLayout android:id="rowsSet1">
    <TableRow><!-- some data --></TableRow>
    <TableRow><!-- some data --></TableRow>
    <TableRow><!-- some data --></TableRow>
  </TableLayout>
  <TbaleRow><!-- some data --></TableRow>
  <TableLayout android:id="rowsSet2">
    <TableRow><!-- some data --></TableRow>
    <TableRow><!-- some data --></TableRow>
  </TableLayout>
</TableLayout>

现在我可以通过 findViewById 获取一组行,例如,隐藏它们。