无法从同一 xml 源将多行加载到 TableLayout

cannot load multiple rows into a TableLayout from the same xml source

我有 TableLayout,我想从同一文件动态加载行,即 automationrow.xml 实际上是预定义的 table row.Here 是我想要的行复制。

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/R2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:visibility="visible">

    <android.support.v7.widget.CardView
        android:id="@+id/r2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:alpha="1"
        card_view:cardCornerRadius="5dp">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/heading_layout"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@color/title"
                android:orientation="horizontal"
                android:padding="5dp">

                <TextView
                    android:id="@+id/tv_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:text="Room 2"
                    android:textColor="@color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/room1"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:alpha="1"
                android:background="#292828"
                android:orientation="vertical"
                android:padding="20dp">

                <Switch
                    android:id="@+id/mySwitchr2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="5dp"
                    android:text="bulb1    "
                    android:textColor="#ffffff" />

                <Switch
                    android:id="@+id/mySwitch2r2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/mySwitchr1"
                    android:layout_marginTop="5dp"
                    android:text="bulb2"
                    android:textColor="#ffffff" />

                <Switch
                    android:id="@+id/mySwitch3r2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/mySwitchr1"
                    android:layout_marginTop="5dp"
                    android:text="bulb3"
                    android:textColor="#ffffff" />

                <Switch
                    android:id="@+id/mySwitch4r2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/mySwitchr1"
                    android:layout_marginTop="5dp"
                    android:text="Fan"
                    android:textColor="#ffffff" />

                <SeekBar
                    android:id="@+id/seekBar2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</TableRow>

我在一个片段和 运行 以下代码中执行此操作,但它在仅添加一行并出现以下错误后中断

java.lang.IllegalStateException: 指定的child已经有一个parent。您必须先在 child 的 parent 上调用 removeView()。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.controls, container, false);        
    TableLayout Tab =(TableLayout) rootView.findViewById(R.id.table);
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.automationrow, container,false);
    TableRow newr = (TableRow) view.findViewById(R.id.R1);
    for (int i=1;i<=2;i++){
        Tab.addView(newr);
        Log.d("check runs",String.valueOf(i)); //printed only once
        //Tab is the TableLayout
    }
}

试试这个

 for (int i=1;i<=2;i++){

     View view = LayoutInflater.from(getActivity()).inflate(R.layout.automationrow, container,false);
     TableRow newr = (TableRow) view.findViewById(R.id.R1);
     Tab.addView(newr);
     Log.d("check runs",String.valueOf(i)); //printed only once
     //Tab is the TableLayout
}