複数の線形レイアウトが事前に定義されている特定のレイアウトを置き換えたり、事前定義された各レイアウトに追加したりするカスタムビューを作成しようとしています。Relativelayoutの異なるレイアウトにビューを追加してxmlを膨らませます
私の定義済みのレイアウトには、relativelayout内に4つの線形レイアウトが親として含まれ、Relativelayoutを拡張し、次のようにビューを初期化するクラスでこのxmlを膨張させます。
private void initView(Context context) {
//Inflate and attach your child XML
View.inflate(context, R.layout.layout_empty_views, this);
llTop = (LinearLayout) findViewById(R.id.linearLayout);
llLeft = (LinearLayout) findViewById(R.id.linearLayout2);
llBottom = (LinearLayout) findViewById(R.id.linearLayout3);
llRight = (LinearLayout) findViewById(R.id.linearLayout4);
}
ここでは12個の子ビューを受け入れ、各直線レイアウトに3個の子を追加する必要があります。 addView関数では、インデックスによってビューを分割できないため、常に-1です。
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (llTop == null) {
super.addView(child, index, params);
} else {
//Forward these calls to the content view
llTop.addView(child, index, params);
}
}
onLayout関数では、定義済みのレイアウトが膨らんでいるため、getChildCountが12より大きくなります。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//super.onLayout(changed, l, t, r, b);
// TODO Auto-generated method stub
Log.v("Count::", String.valueOf(getChildCount()));
/*for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
removeViewAt(i);
//llTop.addView(v);
if (i < 3) {
llTop.addView(v);
} else if (i > 2 && i < 6) {
llRight.addView(v);
} else if (i > 5 && i < 9) {
llBottom.addView(v);
} else if (i > 9) {
llLeft.addView(v);
}
}*/
}
はい、このビューを別のレイアウトに移動するにはどうすればよいですか – chiru