2017-06-13 179 views
0

我想设置一个空白的relativeLayout,然后开始从另一个relativeLayout添加一些视图。Android:如何从不同的布局添加视图

我想这样做,因为此刻我在单个布局中有很多位图会导致内存错误。所以我想实现添加和删除视图的效果,因为我认为在代码中是合适的。

目前我在onCreate()上使用setContentView()布局,这使得我有内存错误,因为有太多的视图要一次添加。

另一种方式。也许这是可能setContentView()的布局与太多的意见。只有在我编写代码以添加特定视图之前,才能添加任何视图。

setContentView(R.layout.start_up_screen_denmark); 
// This will add all the views in the layout causing a memory error. Making everything below irrelavant. 
// So perhaps there is a way to set the ContentView without adding all the views automaticly. 
// Perhaps i can set a blank layout and add views to that layout from the other layout at will. 

ImageView denmark = (ImageView) findViewById(R.id.GoDenmark); 
ViewGroup parent = (ViewGroup) denmark.getParent(); 
parent.removeView(denmark); 
+0

在静态视图(如ImageView)中替换图像不是更容易吗? –

+0

我在这里关于android的整个内存问题。但我读了几篇文章,如果你想避免内存泄漏,那么不要设置任何静态。 – Nulle

回答

0

使用的LinearLayout可能更适合你正在试图做的,因为你可以很容易地lin_lay.addView(View child, int index)lin_lay.removeViewAt(int index)添加和删除一个的LinearLayout的意见是什么。

但是,如果您在一个布局中有很多位图并且导致内存问题,您也可能想要查看RecyclerView

+0

问题是不从当前布局中删除或添加视图。它将设置布局而不一次添加所有视图。也许我误解你的意图。你能给我一个你想做什么的代码示例吗? – Nulle

+0

也许我首先误解了你。你可以发布你想要做的代码吗? –

+0

完成。希望它有一点帮助。 – Nulle

相关问题