2016-06-10 114 views
0

我在我的TableLayout中有很多行,我想访问按钮视图。示例如下:子元素的孩子

<TableLayout> 
    <TableRow> 
     <Button /> 
     <Button /> 
    </TableRow> 
    <TableRow> 
     <Button /> 
     <Button /> 
    </TableRow> 
</TableLayout> 

从表格布局中,我想通过按钮循环。

TableLayout layout=(TableLayout) findViewById(R.id.Layout); 
layout.getChildCount; 

以上仅返回数字表行视图。

+0

你会需要一些递归这个 –

+1

你为什么不给ID属性为每个按钮和通过tableLayout.findViewById(R.id.yourButtonId)访问它? – Talha

+0

谢谢,也将很好地工作,因为每个按钮都有一个唯一的ID ... –

回答

3

试试这个样子,这可能是帮助你

TableLayout layout = (TableLayout) findViewById(R.id.Table_ID); 
for (int i = 0; i < layout.getChildCount(); i++) { 
View child = layout.getChildAt(i); 

if (child instanceof TableRow) { 
    TableRow row = (TableRow) child; 

    for (int x = 0; x < row.getChildCount(); x++) { 
     View view = row.getChildAt(x);//Here you get Your Button View 

    } 
} 
} 
+0

@the_big_blackbox永远wlc –

0

试试这个:

TableLayout layout=(TableLayout) findViewById(R.id.Layout); 
for(int i=0;i<layout.getChildCount();i++) { 
    if (layout.getChildAt(i) instanceof TableRow) { 
     TableRow tableRow = (TableRow) layout.getChildAt(i); 
     for(int j=0;j<tableRow.getChildCount();j++) { 
      if (tableRow.getChildAt(j) instanceof Button) { 
       Button button = (Button) tableRow.getChildAt(j); 
       //your button is here 
      } 
     } 
    } 
}