让所有孩子都进入LinearLayout

时间:2015-04-09 01:38:06

标签: java android android-layout android-linearlayout programmatically-created

如何让所有孩子以后再使用?

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;
}

1 个答案:

答案 0 :(得分:2)

您可以使用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
}