【问题标题】:Getting all children in a LinearLayout在 LinearLayout 中获取所有子项
【发布时间】:2015-04-09 01:38:05
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-12-08
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多