1

我正在创建自定义布局(扩展FrameLayout)。我有一堆在xml中定义的视图(现在没关系)。
我需要做什么。我的自定义布局具有自定义的属性,我们假设它名为footer_banner_type
我有不同的横幅班,其中一些我很不同,因此我不能在xml中放置一些基本横幅。所以我必须添加一些基于属性值的横幅。
我在延长FrameLayout。我是新手,这是我的第一个自定义布局。如何在运行时自定义布局中添加视图

我不知道如何提高性能。
据我了解布局迭代和充气所有子视图。但是如果我需要在运行时添加视图。我不想让布局重申视图层次结构,因为这会是性能问题。
我的问题是如何更好地实施我的任务。

回答

0
//First create your view: 
View wonderfulView = new View(this.getApplicationContext()); 
//Then, create it's LayoutParams 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1); 
//Set those layout params in your view 
wonderfulView.setLayoutParams(params); 
//finaly, add the view to your ViewGroup 
yourLayout.addView(wonderfulView); 

就是这样。

如果要更改视图的容器,你必须将其删除形成以前的母公司是这样的:

View movingThing = ((YourLayoutClass)findViewById(R.id.currentContainer)).getChildAt(WhereYourViewis); 
((YourLayoutClass)findViewById(R.id.currentContainer)).removeView(movingThing); 
((YourLayoutClass)findViewById(R.id.newContainer)).addView(movingThing);