============ Problem Description ============
Help....
I have two XML layout files, a, B, the controls in the two layout files are the same, the IDs are the same. In this way, the ID of the R file generated by Eclipse is also free, so how does Android find the specified control based on this ID, instead of finding another????
============ Solution 1============
Reference 3 Floor u013818990 's reply:
Quote: Referring to the reply of the 2 floor kifile:
First, before you call Findviewbyid, you must have set the Setcontentview in the activity, or overloaded the Oncreatedview method in Fragment, for Findviewbyid, he can only use the Vie W or activity, for view, you traverse the root node is the corresponding view, for the activity, your corresponding root node is you use Setcontentview initialization layout
When you call Findviewbyid Yes, Android first compares whether it has that ID, then returns itself, not whether to judge if it is viewgroup, or if it iterates over the sub-view, otherwise returns NULL, traversing, in order from top to bottom one by one calendar, As soon as the ID of a node is found, the view represented by the node is returned, for example, if you have two view of the same ID in your layout, then the first one must be returned.
What if the XML layout with two IDs is in the same layer (the same depth) then is the XML returned? Or is it the order in which it is traversed, followed by, or ordered? Or does the traversal order follow the order of the XML layout files in ViewGroup? Or is it actually shown in the layout of top to bottom, left to right?
First order, because the system reads always according to your layout file from top to bottom read, then regardless of your layout level, the first control in the time is always limited to read
============ Solution 2============
You can look at the source code of Findviewbyid, there is a more detailed explanation, it will automatically traverse the current view tree to find the corresponding control.
How does Android find the control based on the ID?