我有TableLayout
并且我想从同一个文件动态加载行,即automationrow.xml
,这实际上是一个预定义的表行。这是我想要复制的行。
<?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:指定的子级已有父级。您必须首先在孩子的父母上调用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
}
}
答案 0 :(得分:1)
试试这个
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
}