儿童元素的孩子

时间:2016-06-10 09:39:42

标签: java android xml

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

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

从表格布局我想循环浏览按钮。

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

以上仅返回数字tablerow视图。

2 个答案:

答案 0 :(得分: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

    }
}
}

答案 1 :(得分: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
            }
        }
    }
}