简体   繁体   English

使所有子级进入LinearLayout

[英]Getting all children in a LinearLayout

How to get all children in order to use them later? 如何让所有孩子以后使用它们?

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

You can get the number of children on your layout using the getChildCount() Once you have that you can loop through them like this: 您可以使用getChildCount()获得布局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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM