Android ViewGroup 是一种布局容器,用于管理子视图的位置和大小。我们可以通过以下几种方式来管理子视图:
ViewGroup.addView(View child)
ViewGroup.removeView(View view)
ViewGroup.removeViewAt(int index)
ViewGroup.getChildAt(int index)
ViewGroup.getChildCount()
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
childView.setLayoutParams(params);
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
// 设置子视图的位置和大小
child.layout(left, top, right, bottom);
}
}