当前位置: 代码迷 >> Android >> 在布局中多次添加自定义视图
  详细解决方案

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

热度:18   发布时间:2023-08-04 10:12:53.0

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

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

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

然后我动态创建了布局(相对),并夸大了自定义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] */ 

这就是它对我的影响......

在此之前,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> 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将以下id放在前面的id之下。

你必须自己实现每个孩子

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

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

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);
     }
   }
}

表单的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flxform"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:background="@android:color/transparent"
    android:orientation="vertical" >
</LinearLayout>     

然后,您可以实例化一个“Form”Objekt并将其添加到ScrollView中

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

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

以下是使用RecyclerView和自定义适配器的一个很好的示例:

我希望这有帮助。

  相关解决方案