Android: 如何从不同的布局添加视图
Android: How to add Views from a different layout
我想设置一个空白的 relativeLayout,然后开始从另一个 relativeLayout 添加一些视图。
我想这样做是因为目前我在一个布局中有很多位图,这会导致内存错误。所以我想在代码中实现我认为合适的添加和删除视图的效果。
目前我在 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);
使用 LinearLayout 可能更适合您尝试做的事情,因为您可以使用 lin_lay.addView(View child, int index)
和 lin_lay.removeViewAt(int index)
.
轻松地从 LinearLayout 添加和删除视图
但是,如果您在一个布局中有很多位图并且会导致内存问题,听起来您可能还想查看 RecyclerView。
我想设置一个空白的 relativeLayout,然后开始从另一个 relativeLayout 添加一些视图。
我想这样做是因为目前我在一个布局中有很多位图,这会导致内存错误。所以我想在代码中实现我认为合适的添加和删除视图的效果。
目前我在 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);
使用 LinearLayout 可能更适合您尝试做的事情,因为您可以使用 lin_lay.addView(View child, int index)
和 lin_lay.removeViewAt(int index)
.
但是,如果您在一个布局中有很多位图并且会导致内存问题,听起来您可能还想查看 RecyclerView。