Android的:如何获得父视图的ID?视图、如何获得、Android、ID

2023-09-07 16:23:57 作者:嚼情

View.getRoot()收益查看,所以我们可以很容易地找出哪些是根视图使用 getResourceName(View.getId())

View.getRoot() returns View, so we can easily figure out which is the root view by using getResourceName(View.getId()).

View.getParent() ......,而我希望它也返回查看这是父,实际上只返回 ViewParent 的实例,似乎有非常非常少用的方法/字段。它吮吸。

View.getParent()..., while I expect it also returns View that is the parent, actually only returns an instance of ViewParent that seems to have very very few useful method/fields. It sucks.

那么,有没有什么办法可以知道父母的ID?我相信一个查看的母公司也是查看,因此,它应该具有MID领域。

So, is there any way to know the ID of the parent? I believe a View's parent is also View, thus it should has mID field.

我真的不知道为什么谷歌没有让 View.getParent()只返回查看。这是有道理的,只有当别的其他东西比查看可能是父母,而据我所知,这是仅限于查看和它的子类。

I really wonder why Google didn't let View.getParent() just returns View. It makes sense, only when something else other than View could be the parent, and as far as I know, it's limited to View and its subclasses.

推荐答案

ViewParent 就是这样的观点,即可以有孩子实现一个接口。大部分的时间你回来的类将是的ViewGroup ,如的LinearLayout 或的RelativeLayout 。我不完全相信的名你的意思,但如果你想要得到的类名,你可以这样做总是: view.getParent()的getClass()的getName()

ViewParent is just an interface that any View that can have children implements. Most of the times the class you get back will be an instance of a ViewGroup, like LinearLayout or RelativeLayout. I'm not exactly sure what you mean by "name" but if you want to get the class name you can do so like always: view.getParent().getClass().getName().

 
精彩推荐
图片推荐