how does multiple component with same id work in android?

后端 未结 3 974
再見小時候
再見小時候 2021-01-23 04:59

I have defined several layouts, where few id\'s are multiple defined. I am confused how does it work? why doesn\'t it give error just like we get in java code? and most importan

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 06:02

    Different View instances can have the same ID. This situation may arise when:

    • The instances are inflated from the same xml definition. (Eg.: A custom view has a subview with a given ID. If you use this custom view many times on a layout, the subviews will have the same ID) Custom View which has subviews with same id
    • The instances are inflated from different xml files which contain the same ID. two views with same id
    • The ID is set by code: setId(42) Android: View.setID(int id) programmatically - how to avoid ID conflicts?

    The ID is just a tool what you can use to find Views. It's practically unique in most cases, but not guaranteed.

提交回复
热议问题