在 LinearLayout 中获取所有 children

Getting all children in a LinearLayout

如何获取全部children以便以后使用?

LinearLayout createRow(LinearLayout parrent, int id, int orientation, int color){
    LinearLayout cell = new LinearLayout(this);
    LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    rowParams.weight = 1;
    rowParams.setMargins(5,2,5,2);

    cell.setLayoutParams(rowParams);

    cell.setBackgroundColor(color);
    cell.setOrientation(orientation);
    cell.setId(id);

    parrent.addView(cell);

    cell.setOnClickListener(cellListener);
    return cell;
}

您可以使用 getChildCount() 获取布局上的子项数量 一旦你有了它,你就可以像这样遍历它们:

int childCount = cell.getChildCount();
View child;
for(int i=0; i++; i<childCount)
{
   child = cell.getChildAt(i);
   // do what you want with each child element
}