public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context) {
super(context);
this.initOtherComponent(context);
}
private void initOtherComponent(Context context) {
Button aBtn = new Button(context);
// set id 1
aBtn.setId(1);
aBtn.setText("a btn");
this.addView(aBtn);
Button bBtn = new Button(context);
// set id 2
bBtn.setId(2);
bBtn.setText("b btn");
this.addView(bBtn);
}
@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);
switch (child.getId()) {
case 1:
// 1 is aBtn
Log.d("MyViewGroup", "btn1 setting");
Log.d("MyViewGroup", "r="+r+":l"+l+":t"+t+":b"+b);
child.setVisibility(View.VISIBLE);
child.measure(r - l, b - t);//!!!!!
child.layout(0, 0, child.getMeasuredWidth(), child
.getMeasuredHeight());
break;
case 2:
// 2 is bBtn
Log.d("MyViewGroup", "btn2 setting");
child.setVisibility(View.VISIBLE);
child.measure(r - l, b - t);
child.layout(0, 50, child.getMeasuredWidth(), child.getMeasuredHeight() + 50);
break;
default:
}
}
}
}
android佈局的學習(VG)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.