简体   繁体   中英

Android layout resource id aliasing

I wrote an application that uses two different fragments to render different video sources. I have a separate layout file for both fragments and they have pretty similar structure (the only differences is the video surfaces and a button) in both layout files I have the same buttons, textviews and so on... I tested the application and seems to work fine but now I noticed that the fragments UI elements share the same id.... Inspection and other tools did not warn or signal anything but I'm wondering if this's a bad habit or not.

No problem. just be sure to inflate the right layout file in onCreateView method of the fragment so the fragment class just knows the view and the ids which are defined in the inflated layout file.
But it is a good practice not to use same ids for views cause there might cause IDE autocomplete confusions for you in the future but in general no it's ok.

There's nothing wrong in doing that but its recommended to use different IDs because on the long run with more layouts things might get complicated to differentiate, specially when using autocomplete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM