• 首页
  • 发布文章
  • 我的文章
  • 我的收藏
  • 设置
  • 退出

android子view获取父布局,Android获取布局父ID(Android get layout parent id)_weixin_39561577的博客-CSDN博客

亮 2023-06-15 14:42:43
收藏
编辑
上架
下架

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接口,则

本文转自 https://blog.csdn.net/weixin_39561577/article/details/117484821,如有侵权,请联系删除。