这是我的 Prent 布局(main.xml)
<!-- ============================================================ -->
<!-- All Employees are added here (added and More then one) -->
<!-- ============================================================ -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<!-- ++++++++ here extra layout is added programmatically -->
</LinearLayout>
这是我作为孩子想要的另一个布局文件,比如 child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_left"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:maxLines="2"
android:text="hello"/>
<TextView
android:id="@+id/list_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:text="Photos"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btn_right"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
现在在我的主要活动中,我正在调用 main.xml,并且基于 for 循环条件,我想添加 child.xml 的布局,并且每次我想设置 child.xml 的 TextView 的不同值时
那么它怎么可能呢?
我已经这样做了:
private void doCalculationForMultipleEmployee() {
singleEmployee.setVisibility(View.GONE);
for (int i = 0; i<=tempEmployerList.size()-1; i++) {
View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);
((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);
// customize repeatedLayout with other data
myLinearLayout.addChild(repeatedLayout);
}
}
但在这样做之后,我在 .inflate 和 .addChild 处出现语法错误
那么我哪里出错了?请通过一些代码帮助我通过for循环添加它。