gpt4 book ai didi

java - 让所有 child 都进入 LinearLayout

转载 作者:行者123 更新时间:2023-11-29 20:49:30 24 4
gpt4 key购买 nike

如何获取所有 child 以便以后使用它们?

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
}

关于java - 让所有 child 都进入 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com