在作android UI 的时候,遇到了一个问题,由于不一样的UI之间须要相互切换。因此不加思索的写了下面的程式android
public class FirstLayout extends LinearLayout {}布局
public class SecondLayout extends LinearLayout {
public SecondLayout (Context context) {
super(context);
initial(context);
}
void initialize(Context context) {
initial(new FirstLayout(contextt));
}
}rem
在这种状况下,好比说,咱们点击button,页面进行跳转。可是会进行不断的入栈操做,最终致使stack overflow.页面布局
因此为了不这种状况的出现,要进行另一种操做,在定义个总体布局,而后分开处理it
public class FatherClass extends LinearLayout{event
private LinearLayout sonLayout = null;class
private LinearLayout daughterLayout = null;List
private Button AButton = null;view
private Button BButton = null;vi
public FatherClass (Context context) {
supper(context);
initial(context);
}
initial(Context context) {
AButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
removeView(sonLayout )
addView(daughterLayout);
}
});
BButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
removeView(daughterLayout)
addView(sonLayout);
}
});
}
/*generate sonLayout*/
void addSonLayout(Context){
sonLayout = new LinearLayout(context);
.........
}
/*generate daughterLayout*/
void addDaughterLayout(Context){
daughterLayout = new LinearLayout(context);
.........
}
}
其中,用的是removeView ,删除这样一个子布局,而不是用removeAllViews。这样作的好处就是能够作到局部处理。
从而避免了嵌套入栈操做。因此这一点在多页面布局的时候应该考虑到。