How can I use duplicate IDs in different layouts?

前端 未结 5 474
太阳男子
太阳男子 2020-12-17 15:59

I have two different layouts for two different Activities. There is a button in each of these layouts with the same id: \"@+id/btnOK\". When I set a property for one of thes

5条回答
  •  心在旅途
    2020-12-17 16:30

    On the "Duplicate Ids in layouts" topic, extracted from android developers

    Defining IDs for view objects is important when creating a RelativeLayout. In a relative layout, sibling views can define their layout relative to another sibling view, which is referenced by the unique ID.

    An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

    Which means different layouts may declare identical IDs, tho it's not a best practice.

提交回复
热议问题