java - 子元素的子元素

标签 java android xml

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

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

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

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

以上仅返回表格行 View 数。

最佳答案

试试这样,可能对你有帮助

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

    }
}
}

关于java - 子元素的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37744926/

相关文章:

java - JSF 注销页面 - 最佳实践 : is it correct to use one common bean?

java - 在 Maven 中使用 FindSecBugs 会抛出 java.lang.OutOfMemoryError

java - 为什么不重绘整个 JPanel?

android - 如何在 Telegram 上分享音频

java - 如何在容器 fragment View 内创建具有全屏和纵向功能的 YouTube View

java - 耶拿 ARQ 中子选择的绑定(bind)值存在问题

android - 如何从Prod构建风格中删除库?

python - 阅读,修改 xml 使用 python 将其保存在新的 xml 中

java - 泛型中的泛型

python - 使用 ElementTree 解析 XML