Android “hook”ContentView

xiaoxiao2021-02-28  7

日前在做自动化测试和性能优化,有一个场景需要拿到activity顶层View的生命周期回调,类似hook一下contentview,下面是是想到的方法。

1. 第一步要搞明白activity的view树形结构:

最上层是DcorView,依次为LinarLayout >FrameLayout>ContentView。

2.搞清view的树形结构以后,想办法在FrameLayout和ContentView中间加一层,类似于:

其中:ViewParentInsertOverLayContentView 是一个自定义的view 继承FrameLayout。众所周知,contentView已经有了一个“父亲”,直接通过addView的方式会报错。

The specified child already has a parent. You must call removeView().

现在就调用contentview的“父亲”:FrameLayout的removeAllViews()方法,把包括ContentView在内的所有的view都移除。

然后把contentview加到ViewParentInsertOverLayContentView中,再把ViewParentInsertOverLayContentView 加到”老父亲“FrameLayout中(现在成”爷爷“了,呵呵)。

转载请注明原文地址: https://www.6miu.com/read-1150075.html

最新回复(0)