R.java 是否强制我的所有布局元素根据其 ID 成为公共变量?

发布于 2024-12-19 09:55:28 字数 435 浏览 4 评论 0原文

我假设每个布局的元素 ID(例如按钮、文本视图、编辑文本等)对于该布局来说都是唯一且私有的。话虽如此,您将 Activity 设置为使用特定布局,您应该只能根据该特定布局中设置的 id 找到视图 id。

但是,我现在发现我可以从 Activity 中引用我想要的任何视图 ID,无论我使用“setContentView(R.layout.THELAYOUTIWANTTOTARGET)”指定的物理布局如何。这种行为正常吗?我认为上面指定的布局上只有这些元素的 id 可用?看看 R.java,我相信我在所有布局上指定的所有 id 都是公共变量,因此,没有 id 可以具有相同的名称,否则可能会发生意外行为!

这对我的项目的影响是我现在必须将所有布局中的所有 id 更改为唯一。我想出了我的供应商布局>标题文本视图将与我的客户布局不同> title textview,不是这样的吗?

谢谢您的澄清!

I assumed that each layout's element id's such as buttons, textviews, edittexts, etc. were unique and private to that layout. This being said, you set the Activity to use a certain layout, you should only be able to find view id's based on the id's set in that specific layout.

However, I am finding now that I can reference whatever view id I want from my Activity regardless of the physical layout I have specified with "setContentView(R.layout.THELAYOUTIWANTTOTARGET)". Is this behavior normal, I figured only id's of those elements on the layout I specified above would be available? Looking at R.java, I believe all the id's I specified on all layouts are made public variables, thus, no id's can have the same name or unexpected behavior may occur!

The implications for this on my project is that I must now change all id's in all layouts to be unique. I figured my supplier layout > title textview would have been different from my customer layout > title textview, is this not the case?

Thank you for clarifying!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2024-12-26 09:55:28

你的假设是正确的。 IDS 是全局性的,对于大型项目来说,有时会很痛苦,导致 ID 名称很长。但这也可以用作一个优势,因为您可以在不同的活动上重复使用布局。

例如,您可以为活动的特定部分(例如自定义按钮栏)设置一个布局,您可能希望将其添加到多个活动中。在这种情况下,您可以将其膨胀到第一个 Activity 的特定 ViewGroup 中,以及另一个 Activity 中的另一个 ViewGroup 中。然后,可以在这两个活动中重复使用根据 id 访问特定按钮的方法。

findViewById 方法仅适用于您从中调用它的 Activity。如果一个按钮(或任何其他组件)存在于不同的活动中,则仅返回当前活动中的按钮。

Your assumption is correct. IDS are global, and for large projects it's sometimes a pain, leading to very long ID names. But that can also be used as an advantage, as you can reuse layouts on different activities.

For example, you can have a layout for a specific part of your activity (a custom buttons bar for example) that you may want to add to several activities. In this case, you can just inflate it into a specific ViewGroup of the first activity, and also in another ViewGroup in another activity. The methods to access specific buttons based on their ids can then be reused in both activities.

The method findViewById will only work on the activity you call it from. If a button (or any other component) exists on different activities, only the one in your current activity will be returned.

蔚蓝源自深海 2024-12-26 09:55:28

是的,这很正常。据我所知,没有办法改变它。

所以是的,不同布局中的视图必须有不同的名称的含义是正确的。

编辑:

实际上......划掉它。我的印象是这是必要的,但根据

http://developer. android.com/reference/android/view/View.html

甚至没有必要让 id 在单个文件中是唯一的。只需确保您没有在具有多个相同 id 的树中搜索,否则您将始终得到第一个出现的结果。

这也是有道理的,因为视图在 R 中是否具有相同的 ID 并不重要。以后我会牢记这一点。

Yes, it is normal. As far as I know, there is no way to change it.

So yes, the implication that you will have to have different names for views in different layouts is correct.

EDIT:

Actually... scratch that. I was under the impression that it was necessary, but according to

http://developer.android.com/reference/android/view/View.html

it isn't even necessary to have id's be unique in a single file. Just make sure you aren't searching in a tree that has multiple ids that are the same or you will always get the first occurrence.

It makes sense too since it doesn't really matter if the views have the same ID in R. I will keep this in mind going forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文