<include> tag override attribute
If I have one layout file => res/layout/item.xml :
<?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="60dp" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="John" /> <TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="26dip" android:text="29" /> </LinearLayout> And I have another layout file whose contents contain several elements above = = res/layout/main.xml :
<?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="60dp" android:orientation="vertical" > <!--How can I override "name" and "age" attribute in the included item layout--> <include android:id="@+id/student" layout="@layout/tem" /> <include android:id="@+id/teacher" layout="@layout/item" /> <include android:id="@+id/other" layout="@layout/item" /> </LinearLayout> As you can see above, I use the tag <include> to turn on the same layout. But how can I override the text value " name " and " age " in the included item.xml from main.xml for each included item?
+4
2 answers
change your item.xml as follows:
<?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="60dp" > <TextView android:tag="item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="John" /> <TextView android:tag="item_age" android:layout_width="fill_parent" android:layout_height="26dip" android:text="29" /> </LinearLayout> Then:
//find the container which hold student: ViewGroup containerStudent = findViewById(R.id.student); //find the child: TextView student_name =(TextView)containerStudent.findViewWithTag("item_name"); -one