How can I use duplicate IDs in different layouts?

前端 未结 5 464
太阳男子
太阳男子 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:39

    To me it looks all right (and sometimes quite natural) to use duplicate ids, as long as you do it correctly: instead of Activity.findViewById() which always returns the first matching view, use ViewGroup.findViewById() (ViewGroup can be LinearLayout, FrameLayout etc) which returns view withing the view group.

    To avoid lint warnings:

    1. (Preferred) Place content of the ViewGroup in separate layout XML and use

        
      

      This way you can have several includes of same layout with different parent ids (but same child ids) and lint will never complain.

    2. Disable Lint warning for the project by unticking the box "Duplicate ids within a single layout" in *Settings/Editor/Inspections*

提交回复
热议问题