在布局中多次添加自定义视图

时间:2015-10-19 08:15:45

标签: android xml layout

我有一个自定义XML文件。我想在布局(比如说相对)中多次重复这个动态(显然)。

我看过很多帖子,但都没有帮助。我不是在寻找ListViewAdapters左右。它就像 - RelativeLayout一样简单。在其中,将自定义XML添加到另一个上面。任何次数。

使用静态LinearLayout(垂直方向)时,动态添加视图会导致渲染一次,而不是另一个渲染。不知道为什么。虽然TextView左右在LinearLayout(垂直)内的循环中重复一个在另一个之下。

然后我动态创建了布局(Relative),并夸大了自定义XML。显示一个。当我在第一个下面尝试另一个时,它告诉我先删除孩子的父母(例外)。如果我这样做并再次添加,它就像删除第一个渲染视图并再次添加它一样好。

那么如何在同一布局中获得多个视图呢?

粗略介绍我的尝试:

 mainLayout = (RelativeLayout)findViewById(R.id.mainlay); //Mainlayout containing some views already

        params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW,R.id.sideLayout); //sideLayout is an existing LinearLayout within the main layout.



        View child = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);

        RelativeLayout r1 = new RelativeLayout(this);

        r1.setLayoutParams(params);
        r1.addView(child);
        mainLayout.addView(r1);
        mainLayout.setLayoutParams(params);
        mainLayout.addView( child);
       /* r2 = new RelativeLayout(this);
        r2.setLayoutParams(params);
        r2.addView(contentLayout); [Gives exception] */ 

3 个答案:

答案 0 :(得分:3)

这对我有用......

在此之前,android的问题是:

如果您在LinearLayout(水平)内添加动态视图,它们将水平显示新的已创建实例,并添加到视图中。

然而,令人震惊的是,在LinearLayout(垂直方向)的情况下,情况并不相同。因此整个混乱。

<强>解决方案:

RelativeLayout布局文件与变量绑定,有点像这样:

customLay = (RelativeLayout) mainLay.findViewById(R.id.dynamicCustomLayout);

然后,创建了Dynamic RelativeLayout,其中添加/包装了前一个变量。

customLayout = new RelativeLayout(this);
customLayout.addView(customLay);

为每个布局分配一个ID:

 customLayout.setId(i);

然后运行循环(如果i = 0且i> 0,则为2)

对于i&gt; 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);

当然,代码不仅仅是这个。我已经写了帮助我实现目标的基本要点,并可能在将来帮助其他人。

所以主要的本质是---

  1. 使用Dynamic RelativeLayout
  2. 绑定静态RelativeLayout
  3. 将id分配给Dynamic RelativeLayout包装器和
  4. 根据ID使用RelativeLayoutParameters放置以下内容 ids低于以前的。

答案 1 :(得分:1)

你必须自己实现每个孩子

$error

//好吧,我在我的应用程序中做了一个类似的事情。这是代码:

View child = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);
r1.addView(child);
View child2 = getLayoutInflater().inflate(R.layout.dynamiccustomlayout,null);
r1.addView(child2);

表单的XML

public class FlxForm extends LinearLayout {
    public FlxForm(Context context) {
      super(context);
      inflater = LayoutInflater.from(context);
      inflater.inflate(R.layout.flxform, this);
      this.setPadding(0, 0, 0, 0);
      container = (LinearLayout) this.findViewById(R.id.flxform);
      this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
      //here is my funtion to calculate the items i want to add, its a little bit too complicated, but in the end it works like:
      for(int i=0;i<10;i++){

         View x = inflater.inflate(R.layout.dynamiccustomlayout,null);
         container.addview(x);
     }
   }
}

然后你可以实例化一个&#34; Form&#34; Objekt并将其添加到ScrollView

答案 2 :(得分:0)

执行此操作您必须将RelativeLayout嵌套在ScrollView中并手动管理所有滚动,项目添加,内存管理等。

因此添加n个自定义视图的简单解决方案是使用RecyclerViewListViewGridView等,使用整齐的CustomAdapter和自定义视图。

以下是将RecyclerView与自定义适配器一起使用的一个很好的示例:
http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

我希望这有助于。

相关问题