How does Android find the control based on the ID?

Source: Internet
Author: User

============ 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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.