Android开家长布局ID [英] Android get layout parent id
问题描述
我想知道什么是查看和ViewParent之间的区别?我试图得到一个的ImageView
的父的ID,但这个我不能做的:
myImageView.getParent()的getId()。
所以,有另一种方式来获得这个ID?
我想知道什么是查看和ViewParent区别?
块引用>A
查看
是一类和ViewParent
是一个接口。虽然很多公共布局类实现的
ViewParent
接口则无法保证。您遇到的问题是,
myImageView.getParent()
是返回一个ViewParent
这没有按' ŧ直接暴露的getId()
方法。正如其他人所说,铸造
ViewParent
到查看
使用...((查看)myImageView.getParent())的getId()。
...应该在编译的时候工作,但要注意以下几点...
- 如果父
查看
不执行ViewParent
接口,那么中投将失败。- 父
查看
必须在布局文件中定义的资源ID(例如)的android:ID = @ + ID / myParentViewId
或呼叫的getId
将返回空
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 ?
解决方案I would like to know what is the difference between View and ViewParent ?
A
View
is a class and aViewParent
is an interface.Although many of the common layout classes implement the
ViewParent
interface it isn't guaranteed.The problem you're having is that the
myImageView.getParent()
is returning aViewParent
which doesn't directly expose agetId()
method.As others have said, casting the
ViewParent
to aView
using...((View) myImageView.getParent()).getId();
...should work at compile time but be aware of the following...
- If the parent
View
doesn't implement theViewParent
interface then the cast will fail.- The parent
View
must have a resource id defined in the layout file as (for example)android:id=@+id/myParentViewId
or the call togetId
will returnnull
这篇关于Android开家长布局ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!