ID name problem in XML in Android

Source: Internet
Author: User

Case one: The same name in the same XML file

If you have the same name in the same XML file, the previous one is valid, and the second one is invalid

Scenario Two: The same name in a different XML file

The IDs of the different XML files are valid if they have the same name.

When the Android project is getting bigger. XML file more and more, avoid the same component in two XML files using the same ID name, in the Gen directory of the R.java file, the ID of the declaration is in the ID class, that is, public static final class id{} , when the same components in two XML files, such as button, are likely to have id= "@+id/button" in many files, starting to think that referencing duplicate IDs in a Java class causes the program to be unrecognized.

Later, by accident, you just Setcontentview (R.layout.updatemyaddressbook); The IDs in the XML file and the XML file associated with this file are not duplicated, and the Findviewbyid is used in the class ( R.id.button); When the program is not recognized.

This is a very easy question to understand: Because in Android's framework design, each control is subordinate to a control tree, each control is managed and provisioned by its parent control, and the root control is a container control, and all child controls are constructed on top of the root control, thus forming a control field of a control tree. Duplicate names are not allowed in this control field, and the IDs of those controls are not valid, meaning that the child controls in the container control are not allowed to have duplicate names, and the names of the two controls that are not in the same container control do not matter.

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.