【问题标题】:How to add a inflated view multiple times to a linear Layout?如何将膨胀视图多次添加到线性布局?
【发布时间】:2016-06-12 11:46:55
【问题描述】:

我正在尝试在 LinearLayout 容器内添加膨胀视图。但是,我让孩子已经有了父母的问题。 以下是我多次包含容器的 xml 文件。 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@android:color/black"
        ></LinearLayout>
</LinearLayout>

item_button.xml 是我要添加到容器中的 xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="@color/purple"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="Button 1"
        android:padding="20dp"
        android:background="@color/colorAccent"
        />

</LinearLayout>

以下是 onCreate 方法中的 java 代码。我想将 childView 多次添加到容器中。:

        View childView;
        LayoutInflater inflater = (LayoutInflater)   this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        childView = inflater.inflate(R.layout.item_button, null);
        container.addView(childView);
        container.addView(childView);

但是将子多次添加到视图中会出现以下错误:

The specified child already has a parent. You must call removeView()
on the child's parent first.

【问题讨论】:

    标签: android


    【解决方案1】:

    发生这种情况是因为您要多次添加同一个视图,要实现您想要实现的目标,您只需添加一次视图,因此每次您想要将视图添加到布局。

    假设您要添加两次:

    View childView1, childView2;
    LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    childView1 = inflater.inflate(R.layout.item_button, null);
    childView2 = inflater.inflate(R.layout.item_button, null);
    container.addView(childView1);
    container.addView(childView2);
    

    或者,如果您想多次添加视图:

    View v1, v2, v3;
    View[] childViews = new View[]{v1,v2,v3};
    LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for(int v = 0; v < childViews.length; v++){
        childViews[v] = inflater.inflate(R.layout.item_button, null);
        container.addView(childViews[v]);
    }
    

    【讨论】:

      【解决方案2】:

      由于膨胀的视图已经添加到父布局中,因此每次添加之前都需要对其进行膨胀。你的代码应该是这样的:

      View childView;
      LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      for (int i = 0; i < count; i++) {
          childView = inflater.inflate(R.layout.item_button, null);
          container.addView(childView);
      }
      

      其中count 显然是您要添加item_button 布局的次数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多