2013-04-17 70 views
0

我正在开发一个Android应用程序,我使用LayoutInflator来生成新布局。该应用程序的基本功能是 - 它具有一个imageview。当我点击一个按钮时,我将布局更改为显示多个ImageView(如2x2或4x4)。我成功地使用LayoutInflator显示这些布局。但是,之前的布局保持不变,新布局显示而不是旧布局,这种布局会混淆整个布局。我的问题是 - 无论如何要在显示新的布局之前销毁旧的布局?如何销毁布局?

编辑:

这里是代码(在onClick事件,我使用)

public void oneby1onClick(View view){ 
    RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout); 
    view = getLayoutInflater().inflate(R.layout.activity_main, main,false); 
    main.removeView(main); 
    main.addView(view); 
} 

public void twoby2onClick(View view){ 
    RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout); 
    view = getLayoutInflater().inflate(R.layout.twoby2, main,false); 
    main.removeView(main); 
    main.addView(view); 
} 

public void fourby4onClick(View view){ 
    RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout); 
    view = getLayoutInflater().inflate(R.layout.fourby4, main,false); 
    main.removeView(main); 
    main.addView(view); 
} 
+1

后您的应用程序的代码。 – Egor

+3

您不会销毁布局,而是替换它。看起来你做错了,因为通常布局不会出现在彼此之上,除非你真的把它添加在顶部。 – zapl

+0

^+ 1我这样做,只是我有布局上的onclick事件改变像[这个链接提示](http://myandroidsolutions.blogspot.com/2012/06/android-layoutinflater-turorial.html)的按钮。 @zapl - 你如何替换布局? – TheRookierLearner

回答

1

也许你可以只让你的布局不可见的,如果是这样的话。 这是可以做到很容易programmaticaly:

layout.setVisibility(View.GONE); 
+0

'View.GONE!= View.INVISIBLE';) – m0skit0

+0

你绝对不应该使用'View.INVISIBLE'。 –

+0

刚刚尝试过,都是'View.GONE'和'View.INVISIBLE'。这使得整个'View'消失。即使是新的“视图”也没有出现。 – TheRookierLearner

0

据@ ookami.kb的建议我应该用main.removeAllViews();代替main.removeView(main);,帮助我。