Android开家长布局ID [英] Android get layout parent id

查看:183
本文介绍了Android开家长布局ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是查看和ViewParent之间的区别?我试图得到一个的ImageView 的父的ID,但这个我不能做的:

  myImageView.getParent()的getId()。

所以,有另一种方式来获得这个ID?


解决方案

  

我想知道什么是查看和ViewParent区别?


A 查看是一类和 ViewParent 是一个接口。

虽然很多公共布局类实现的 ViewParent 接口则无法保证。

您遇到的问题是, myImageView.getParent()是返回一个 ViewParent 这没有按' ŧ直接暴露的getId()方法。

正如其他人所说,铸造 ViewParent 查看使用...

 ((查看)myImageView.getParent())的getId()。

...应该在编译的时候工作,但要注意以下几点...


  1. 如果父查看不执行 ViewParent 接口,那么中投将失败。

  2. 查看必须在布局文件中定义的资源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 a ViewParent 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 a ViewParent which doesn't directly expose a getId() method.

As others have said, casting the ViewParent to a View using...

((View) myImageView.getParent()).getId();

...should work at compile time but be aware of the following...

  1. If the parent View doesn't implement the ViewParent interface then the cast will fail.
  2. The parent View must have a resource id defined in the layout file as (for example) android:id=@+id/myParentViewId or the call to getId will return null

这篇关于Android开家长布局ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆