android - 监听 Android 中的布局变化

标签 android layout view android-layout

当布局过程完成时,我如何收到通知?是否有像 onLayoutFinished() 这样的东西我可以在某处实现或重写?

来自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.

我想要的是在“第二遍”完成后执行一些逻辑。特别是,我想对一个 View 进行一些更改,然后根据第一个 View 的框架(位置和大小)对另一个 View 进行更改。

最佳答案

您可以简单地重写 onLayout() 并在调用 super.onLayout() 后执行您需要执行的操作。您还可以重写 onSizeChanged(),仅当布局过程更改 View 的坐标/大小时才会调用该方法。

关于android - 监听 Android 中的布局变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4716194/

相关文章:

java - GridBagLayout 不遵守 gridx 和 gridy

java - 如何让一个 Controller 正确调用第二个 Controller 方法来更新第二个 Controller fxml/node?

android - 在 Android 应用程序中实现手势识别

android - 如何在android中使用 'Dropbox API'进行流媒体视频?

android - 制作双倍宽度的屏幕布局

ios - subview 在运行时调整大小 - XCode 4.5.1 iOS

postgresql - Postgres 可编辑联合 View

android - FragmentPagerAdapter 的 getItem() 中的 switch 语句如何工作?

android - Dagger 2 : Unable to inject singleton in other scope

css - 如何让多个样式表在单个页面上工作?