gpt4 book ai didi

android - 父级 wraps_content 和子级 fills_parent 是什么意思?

转载 作者:行者123 更新时间:2023-11-29 22:35:01 26 4
gpt4 key购买 nike

摘自关于 LayoutParams 的 Android 文档:

FILL_PARENT, which means the view wants to be as big as its parent (minus padding)

WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)

现在,如果父级将其高度设置为 wrap_content,而子级将其高度设置为 fill_parent,这究竟意味着什么?我的意思是,这听起来像是尺寸请求之间的“平局”: child 想要和它的 parent 一样大,但 parent 想要和它的 child 一样大。

我读过 How Android Draws Views ,但这篇文章没有解释布局参数和 MeasureSpecs 实际上是如何协同工作的。

最佳答案

基本上 MeasureSpecs 是关于大小的,而 LayoutParams 帮助父 View 在其 subview 之间分配可用大小。
好吧,基本上它以这种方式工作:假设 ViewHierachy P0 - P1 - C0,C1(P 代表父级,C 代表子级)
1) P0 使用一些 MeasureSpecs 调用 P1 的测量值(取决于 P1 的 LayoutParams)
2) 在 P1 的 onMeasure 调用中,P1 从 P0 提供的测量规范中知道它可以有多大
3)P1 需要根据每个 child 的 LayoutParams 告诉它 child 他们的大小,例如:Child (C0) 有 LayoutParams.width = "FILL_PARENT"并且 P1 是垂直对齐的 LinearLayout,所以 P1 可以用一个宽度告诉它 C0 子 MeasureSpec.AT_MOST。为了确定每个 child 的高度,P1 知道: child 数量、每个 child 的 LayoutParams 和每个 child 的重力(如果有的话),以便它可以在它的 child 之间分配可用高度。

希望对您有所帮助,对于更深入的方法,您可以查看 LinearLayout.onMeasure 方法,它有一些注释可以帮助您理解正在发生的事情。

关于android - 父级 wraps_content 和子级 fills_parent 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1679707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com