监听 Android 中的布局变化
布局阶段完成后如何收到通知?是否有类似 onLayoutFinished()
的东西我可以在某处实现或重写?
从 View javadoc:
布局是一个两遍过程: 测量通道和布局通道。这 测量通行证实施于 measure(int, int) 并且是自上而下的 视图树的遍历。每个视图 降低尺寸规格 递归期间的树。在 测量结束,每个视图 已存储其测量值。这 第二遍发生在布局(int, int, int, int) 并且也是自上而下的。 在此过程中,每位家长 负责定位其所有 孩子们使用计算出的尺寸 测量通过。
我想要的是在“第二遍”完成后执行一些逻辑。特别是,我想对一个视图进行一些更改,然后根据第一个视图的框架(位置和大小)对另一个视图进行更改。
How can I get notified when a layout pass has finished? Is there something like onLayoutFinished()
that I can implement or override somewhere?
From the View javadoc:
Layout is a two pass process: a
measure pass and a layout pass. The
measuring pass is implemented in
measure(int, int) and is a top-down
traversal of the view tree. Each view
pushes dimension specifications down
the tree during the recursion. At the
end of the measure pass, every view
has stored its measurements. The
second pass happens in layout(int,
int, int, int) and is also top-down.
During this pass each parent is
responsible for positioning all of its
children using the sizes computed in
the measure pass.
What I'd like is to execute some logic after the "second pass" has finished. In particular, I want to make some changes to a view, and then make changes to another view that depend of the frame (position and size) of the first view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地重写 onLayout() 并在调用 super.onLayout() 后执行您需要执行的操作。您还可以重写 onSizeChanged(),仅当布局过程更改视图的坐标/大小时才会调用该方法。
You can simply override onLayout() and do whatever you need to do after calling super.onLayout(). You can also override onSizeChanged(), which will be invoked only when a layout pass changes the coordinates/size of a View.