这就是我的工作方式…
在此之前,android的问题是:
如果在LinearLayout(水平)内部添加动态视图,则它们将与新创建的实例一起水平显示,并添加到视图中.
但是,令人震惊的是,在LinearLayout(垂直方向)的情况下,情况并不相同.因此整个混乱.
解:
RelativeLayout布局文件已与变量绑定,如下所示:
customLay = (RelativeLayout) mainLay.findViewById(R.id.dynamicCustomLayout);
然后,创建一个动态RelativeLayout,在其中添加/包装前一个变量.
customLayout = new RelativeLayout(this);
customLayout.addView(customLay);
每个布局都有一个ID:
customLayout.setId(i);
然后运行循环(如果i = 0且i> 0的条件为2)
对于i> 0(表示第二个动态布局,要添加到第一个下面),将创建LayoutParameter:
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
然后对于i> 0,使用动态视图的ID,将它们一个添加到另一个下面:
//Following code below used id to place these views below each other to attain list type arrangement of views//
// i==0 for first view on top//
if (i == 0) {
params.addRule(RelativeLayout.BELOW, R.id.sideLayout);
customLayout.setLayoutParams(params);
}
// i>0 for views that will follow the first top//
else {
params.addRule(RelativeLayout.BELOW, i - 1);
customLayout.setLayoutParams(params);
}
然后添加到主根布局,其中需要显示所有这些视图或卡片:
includeLayout.addView(customLayout);
当然,代码不只是这个.我写了一些重要的观点,这些观点可以帮助我实现目标,并可能在将来帮助其他人.
所以主要的本质是-
>使用动态RelativeLayout,
>绑定静态RelativeLayout,然后
>为动态RelativeLayout包装器分配ID,并
>根据ID使用RelativeLayoutParameters放置以下内容
低于先前ID的ID.