如何多次向线性布局添加展开视图?

Joy*_*son 6 android

我试图在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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

以下是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);
Run Code Online (Sandbox Code Playgroud)

但是,将子项多次添加到视图中会产生以下错误:

The specified child already has a parent. You must call removeView()
on the child's parent first.
Run Code Online (Sandbox Code Playgroud)

Muh*_*aat 8

发生这种情况的原因是,您要多次添加同一视图,要实现所需的功能,您只需添加一次视图,因此每次要将视图添加到布局时,都必须创建一个新视图。

假设您要添加两次:

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);
Run Code Online (Sandbox Code Playgroud)

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

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]);
}
Run Code Online (Sandbox Code Playgroud)


Reh*_*han 6

由于已将展开视图添加到父布局,因此您需要在每次添加之前对其进行充气。您的代码应如下所示:

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);
}
Run Code Online (Sandbox Code Playgroud)

这里count明明是你要添加的次数item_button布局。