答案 0 :(得分:48)
答案 1 :(得分:15)
如果您有其他类型视图
TableLayout layout = (TableLayout) findViewById(R.id.IdTable);
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);
view.setEnabled(false);
}
}
}
答案 2 :(得分:2)
我一直在寻找同样的事情。对我来说,我正在寻找如何检查表格布局中的视图。我是这样做的:
//This will iterate through your table layout and get the total amount of cells.
for(int i = 0; i < table.getChildCount(); i++)
{
//Remember that .getChildAt() method returns a View, so you would have to cast a specific control.
TableRow row = (TableRow) table.getChildAt(i);
//This will iterate through the table row.
for(int j = 0; j < row.getChildCount(); j++)
{
Button btn = (Button) row.getChildAt(j);
//Do what you need to do.
}
}
答案 3 :(得分:1)
如果您尝试删除TableRows,ID计数器将减少,所以请使用此循环删除...如果您不想删除,则可以使用上面的一些循环:D
TableLayout table = (TableLayout) findViewById(R.id.tblScores);
for(int i = 0; i < table.getChildCount(); i = 0)
{
View child = table.getChildAt(0);
if (child != null && child instanceof TableRow)
{
TableRow row = (TableRow) child;
row.removeAllViews();
table.removeViewAt(i);
}
}
清除所有行!
我认为你的主要问题是如果你删除了所有行都将被新放置的行,那么当你删除ID = 3的行时,ID = 5的行现在是ID = 4
所以也许您必须重置计数器并再次迭代,或者在清除所有行后生成新表
答案 4 :(得分:0)
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = (TableRow) table.getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}