首页 > 解决方案 > Android布局资源ID别名

问题描述

我编写了一个应用程序,它使用两个不同的片段来呈现不同的视频源。我对两个片段都有一个单独的布局文件,并且它们在两个布局文件中具有非常相似的结构(唯一的区别是视频表面和一个按钮)我有相同的按钮、文本视图等......我测试了应用程序,似乎工作正常,但现在我注意到片段 UI 元素共享相同的 id.... 检查和其他工具没有警告或发出任何信号,但我想知道这是否是一个坏习惯。

标签: javaandroid

解决方案


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.


推荐阅读