-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConstraintLayout 能放在listview里面用吗 #11
Comments
可以 |
`class VisualizeReportedRecordWidget extends StatelessWidget { const VisualizeReportedRecordWidget( @OverRide
} 您好,当我在listview里面使用该布局时,高的wrapContent不起作用,怎么调试都是44.5。请问是有什么限制导致的吗。期待你的答复。 |
你用错了,当你把 ConstraintLayout 的高度设置为 wrapContent 时,ConstraintLayout 的自身大小是由那些宽高为 wrapContent 或 fixed size 的子元素的大小(以及它们的 margin)撑起来的。那些宽高为 matchParent 或 matchConstraint 的子元素不会影响 ConstraintLayout 的自身大小。 如果一部分子元素相对于顶部对齐,一部分相对于底部对齐,是不会把 ConstraintLayout 撑到你的预期大小的。 你的布局方式应该改为始终相对于一个方向去对齐。 |
好的,谢谢您的解惑。 |
钉在这里,供后面的小伙伴参考。 |
稍等我研究下 |
改成这样就好了。宽度从 matchConstraint 改为 matchParent,删除横向的约束 |
这里牵扯到布局优先级的问题,整个 ConstraintLayout 中只有一个子元素,这里要指望 Column 把 ConstraintLayout 撑起来,那么 Column 的宽度就不能再是 matchConstraint,因为这样会让 parent 先布局,Column 后布局。这导致 parent 没有被任何元素撑起来,实际宽高为 0。就导致什么也展示不出来了 。我后面会写一篇原理分析的文章好好讲讲这些 “有坑” 的注意点。 |
约束布局的核心原理是按照优先级来布局,A 约束在 B 的右边,则 A 的布局优先级比 B 低。也就是要等 B 布局之后再布局 A。 你这里在横向上把宽高设为 matchConstraint,导致 Column 和 parent 产生了约束依赖关系,parent 的布局优先级高于 Column。因为要等 parent 的 left 和 right 确定以后,才能确定 Column 的宽度。 当你改用 matchParent 以后,横向上的约束依赖关系就被打破了,因为在布局 Column 时可以拿到 parent 的宽度,也就是 ListView 的宽度。这就使得 Column 可以先于 parent 布局,parent 就能被撑起来,界面展示就恢复正常了! |
其实最主要的还是约束布局的约束推断算法不够聪明,不然你这么写也可以保证不出问题的 |
根据您的详细的回复,我解决了我项目中遇到的问题,谢谢您! |
No description provided.
The text was updated successfully, but these errors were encountered: