监听 Android 中的布局变化

发布于 2024-10-12 07:35:06 字数 516 浏览 1 评论 0原文

布局阶段完成后如何收到通知?是否有类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2024-10-19 07:35:06

您可以简单地重写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文