Android获取布局父ID(Android get layout parent id)
我想知道View和ViewParent有什么区别? 我想获取ImageView父级的Id,但我不能这样做:
myImageView.getParent().getId();
那么有另一种获得此ID的方法吗?
I would like to know what is the difference between View and ViewParent ? I am trying to get the Id of the parent of an ImageView but this I can't do :
myImageView.getParent().getId();
So is there another way to get this id ?
原文:https://stackoverflow.com/questions/22759352
更新时间:2020-01-07 18:13
最满意答案
我想知道View和ViewParent有什么区别?
View是一个类, ViewParent是一个接口。
虽然许多常见的布局类实现了ViewParent接口,但不能保证。
您遇到的问题是myImageView.getParent()返回的ViewParent不直接公开getId()方法。
正如其他人所说,使用...将ViewParent转换为View
((View) myImageView.getParent()).getId();
......应该在编译时工作,但要注意以下事项......
如果父View未实现ViewParent接口,则