The previous version of my question was too long and difficult to understand, so I have rewritten it. If you are interested in the old version, please refer to the edit history.
When a
I have a custom view that increases in height as its content grows. Once it reaches the maximum height allowed by the parent, its width will increase for any additional content (if
“嘿!” 我的视图回答道,“如果你只给我一个最大高度
视图想:“唉,为什么我的父布局不让我更宽呢?明明有足够的空间。也许 Stack Overflow 上的某个人可以给我一些建议。”
When a
RelativeLayout
parent wants to know how big its child view should be, it sends MeasureSpec
s to the child's onMeasure
method in multiple passes.I have a custom view that increases in height as its content grows. Once it reaches the maximum height allowed by the parent, its width will increase for any additional content (if
wrap_content
is selected for the width). Therefore, the width of the custom view depends on the maximum height specified by the parent. Please keep the HTML tags and simply refine the content to make it more understandable. Please do not provide explanations and only provide translations.
一次(不和谐的)亲子谈话
onMeasure
第一次执行
RelativeLayout
父视图告诉我的视图:“你可以有任何宽度最大为900
,任何高度最大为600
。你怎么说?”
我的视图回答说:“好吧,在那个高度下,我可以用100
的宽度来适应所有内容。所以我将采取100
的宽度和600
的高度。”
onMeasure
第二次执行
RelativeLayout
父布局告诉我的视图:“你上次告诉我你需要宽度为100
,那么我们就把它作为确切的宽度。现在,基于这个宽度,你需要什么样的高度?任何最多500
的高度都可以。”“嘿!” 我的视图回答道,“如果你只给我一个最大高度
500
,那么100
的宽度就太窄了。我需要200
的宽度来适应这个高度。但好吧,按照你的方式来吧。我不会违反规则(至少现在还没有...)。我将采取100
的宽度和500
的高度。”
最终结果:
RelativeLayout
父布局为视图分配了最终大小,宽度为 100
,高度为 500
。这当然对于视图来说太窄了,部分内容被裁剪了。视图想:“唉,为什么我的父布局不让我更宽呢?明明有足够的空间。也许 Stack Overflow 上的某个人可以给我一些建议。”