我可以在 Android 的不同布局中使用相同的 id 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12333624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:25:47  来源:igfitidea点击:

Can I use the same id in different layout in Android?

androidandroid-layout

提问by happyhardik

I am new to Android development. Is it fine to use the same ID for images and TextViewsin different LayoutXML files?

我是 Android 开发的新手。对图像和TextViews不同的LayoutXML 文件使用相同的 ID 是否可以?

When eclipse auto-list them for me, it lists all the layout variables from the project, so will it collide? Till now I have not noticed any problems using the same ID in different layouts, but I am concerned in long run.

当eclipse为我自动列出它们时,它列出了项目中的所有布局变量,那么它会发生冲突吗?到目前为止,我还没有注意到在不同布局中使用相同 ID 的任何问题,但从长远来看,我很担心。

回答by Raghav Sood

Short answer: Yes, you can.

简短的回答:是的,你可以。

Long answer: You can do this because whenever you use findViewById() to get a reference to a part of your layout, the method only looks for that view in the currently inflated layout. So even if you have another view with the same ID in another layout, Android will not look for it there.

长答案:您可以这样做,因为每当您使用 findViewById() 获取对布局的一部分的引用时,该方法只会在当前膨胀的布局中查找该视图。因此,即使您在另一个布局中有另一个具有相同 ID 的视图,Android 也不会在那里寻找它。

回答by Chupamobile Evangelist

It is recommended that you use different ids for different layouts. On the long run, when you will have a lot of layouts and therefor a lot of ids it will get very complicated to differentiate them.

建议您为不同的布局使用不同的 id。从长远来看,当你有很多布局,因此有很多 id 时,区分它们会变得非常复杂。

I usually name my ids like this: layoutName_elementId.

我通常这样命名我的 ID:layoutName_elementId.

It works for me to easily find the id I'm looking for, especially when using autocomplete (I know on what layout I'm working, but I don't really know the id; in this case, with my naming strategy, I only type the layout name and it brings on all the ids of that layout).

它对我来说很容易找到我正在寻找的 id,尤其是在使用自动完成功能时(我知道我正在工作的布局,但我真的不知道 id;在这种情况下,根据我的命名策略,我只需键入布局名称,它就会显示该布局的所有 ID)。

More information on layouts and ids can be found here.

可以在此处找到有关布局和 id 的更多信息。

Happy coding,

快乐编码,

回答by Alex Epelde

According to developer API guides:

根据开发人员 API 指南:

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).

ID 不必在整个树中都是唯一的,但它在您要搜索的树部分内应该是唯一的(通常可能是整个树,因此最好尽可能完全唯一)。

So the short answer is that it's not mandatory but it's a good practice to avoid possible conflicts.

所以简短的回答是它不是强制性的,但它是避免可能发生冲突的好习惯。

回答by Pavel Poley

Not recommended, because if in future you will need to refactor the viewid, Android studio will refactor it in all XMLfiles and classes and you will get into trouble.

不推荐,因为如果以后你需要重构viewid,Android studio 会在所有XML文件和类中重构它,你会遇到麻烦。

But there are also some cases when you do need to use same idfor example if you have some abstract and you reuse multiple layouts.

但是在某些情况下,您确实需要使用相同的内容id,例如,如果您有一些摘要并且您重用多个布局。

In case you have multiple viewswith same id'sin your project and you need to refactor, do it manually, don't use build in IDE function, change the idin the target view inside XMLlayout then fix the red error inside layout.

如果您的项目中有多个views相同的id's并且您需要重构,请手动进行,不要使用内置 IDE 功能,更改布局id内的目标视图,XML然后修复布局内的红色错误。