Say you want to include this:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/some_image"
/>
<TextView
android:id="@+id/included_text_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
so in your code you insert it like this:
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
>
<include android:id="@+id/header_1" layout="@layout/name_of_layout_xml" />
<include android:id="@+id/header_2" layout="@layout/name_of_layout_xml" />
</LinearLayout>
now you want to access the text views within the included layouts to set the text dynamically. In your code you simply type:
LinearLayout ll = (LinearLayout)findViewById(R.id.header_1);
TextView tv = (TextView)ll.findViewById(R.id.included_text_view);
tv.setText("Header Text 1");
ll = (LinearLayout)findViewById(R.id.header_2);
tv = (TextView)ll.findViewById(R.id.included_text_view);
tv.setText("Header Text 2");
notice that you use the individual LinearLayouts' findViewById methods to narrow the search to only their children.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…