Reputation: 2009
http://developer.android.com/training/improving-layouts/reusing-layouts.html
In this website it introduce
<include layout="@layout/titlebar"/>
to reuse layout, so I may code like this the problem is ,
<include layout="@layout/titlebar"
android:id="@+id/bar_1"/>
<include layout="@layout/titlebar"
android:id="@+id/bar_2"/>
if the titlebar is a linearlayout, and I would like to get the textview inside titlebar, how can I differenate between bar 1 and bar 2? Thanks
Upvotes: 1
Views: 1772
Reputation: 26007
Try:
// Get root View id from that include link
View yourLayout1 = findViewById(R.id.bar1);
View yourLayout2 = findViewById(R.id.bar2);
// Get text view contained inside the include file
TextView yourTextView1 = (TextView)(yourLayout1.findViewById( R.id.yourInnerTextview ));
TextView yourTextView2 = (TextView)(yourLayout2.findViewById( R.id.yourInnerTextview ));
P.S: I haven't tested it but logically sounds good. So let me know if it works.
Upvotes: 1
Reputation: 1793
/**
* Look for a child view with the given id. If this view has the given
* id, return this view.
*
* @param id The id to search for.
* @return The view that has the given id in the hierarchy or null
*/
public final View findViewById(int id) {
if (id < 0) {
return null;
}
return findViewTraversal(id);
}
View Object also have findViewById function. And it only find the child . So you can firstly find the bar_1 or bar_2 , and then use the bar_1 Object or bar_2 Object's findViewById function to get the child view you want.
Upvotes: 0