I have a main.xml file describing the layout of my main activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
No, there is no way to pass parameters to the included layout other than the layout params using the <include>
directive.
You can inflate the layout programatically and add them to your view. Add an id to the container in your main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" />
Then in your Activity:
ViewGroup container = (ViewGroup)findViewById(R.id.container);
for (int i = 0; i < 6; i++) {
View myLayout = getLayoutInflater.inflate(R.layout.mylayout, null);
TextView tv = myLayout.findViewById(R.id.textView);
tv.setText("my layout " + i);
container.addView(myLayout); // you can pass extra layout params here too
}