开发者

Android, Get a DecorView child

开发者 https://www.devze.com 2022-12-09 01:32 出处:网络
I wonder if it is possible to get a decorViews child. For example if I have a decorView object and I know that the child of the decorView is a FrameLayout, is there someway for me to get the FrameLayo

I wonder if it is possible to get a decorViews child. For example if I have a decorView object and I know that the child of the decorView is a FrameLayout, is there someway for me to get the FrameLayout object just by using its parent?

Obviously I could use getViewById() in the traditional way, bu开发者_开发技巧t if I only want to use the parent object, is it possible to get the FrameLayout object?

Thanks,


Are you talking about object inheritance?

If that is the case, then it is simple: Cast it.

Assuming you have a class "Foo" which is an ancestor-class of "Bar" (in other words: Bar is a "child" of Foo), then take this example:

Foo myFoo = getSomeFoo();
Bar myBar = (Bar)myFoo;

So, adapting this to your question:

View myView = getDecorView();
FrameLayout myFrameLayout = (FrameLayout) myView;

or:

FrameLayout myFrameLayout = (FrameLayout) getDecorView();

You should protect the block making this call though as this will throw a ClassCastException if the instance returned by getDecorView is not a FrameLayout (or a (in)direct subclass of it). Simply add a try/catch block, or add a throwing declaration to the method containing this line. Whatever suits you best.

0

精彩评论

暂无评论...
验证码 换一张
取 消