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