Java 子元素的子元素
我的TableLayout中有许多行,我想访问按钮视图。示例如下:Java 子元素的子元素,java,android,xml,Java,Android,Xml,我的TableLayout中有许多行,我想访问按钮视图。示例如下: <TableLayout> <TableRow> <Button /> <Button /> </TableRow> <TableRow> <Button /> <Button /> </TableRow> </Table
<TableLayout>
<TableRow>
<Button />
<Button />
</TableRow>
<TableRow>
<Button />
<Button />
</TableRow>
</TableLayout>
以上仅返回tablerow视图的数量 像这样试试,可能对你有帮助
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
}
}
}
TableLayout布局=(TableLayout)findViewById(R.id.Table\u id);
对于(int i=0;i
像这样试试,可能会对你有所帮助
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
}
}
}
TableLayout布局=(TableLayout)findViewById(R.id.Table\u id);
对于(int i=0;i
试试这个:
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
}
}
}
}
TableLayout布局=(TableLayout)findviewbyd(R.id.layout);
对于(int i=0;i请尝试以下方法:
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
}
}
}
}
TableLayout布局=(TableLayout)findviewbyd(R.id.layout);
对于(int i=0;i您需要一些递归,为什么不为每个按钮分配ID属性并通过tableLayout.findViewById(R.ID.yourbutonid)访问它呢?谢谢,这也会很好地工作,因为每个按钮都有一个唯一的id。是否可以在按钮的位置使用一个变量?为此,您需要一些递归。为什么不为每个按钮分配id属性,并通过tableLayout访问它。findViewById(R.id.yourButtonId)?谢谢,这也会很好地工作,因为每个按钮都有一个唯一的id。是否可以使用一个变量来代替yourButtonId@the_big_blackbox总是wlc@the_big_blackbox总是环球