Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

android - When will MeasureSpec.UNSPECIFIED and MeasureSpec.AT_MOST be applied?

I am a bit confused about MeasureSpec.UNSPECIFIED and MeasureSpec.AT_MOST. I know that when match_parent or an constant dimension value is set to layout_width or layout_height, MeasureSpec.EXACTLY will be applied when measuring the view.

Then, how about MeasureSpec.UNSPECIFIED and MeasureSpec.AT_MOST?

My understanding is that, when layout_width or layout_height is set to wrap_content, MeasureSpec.UNSPECIFIED will be applied, because wrap_content means the height or width of a view can be any size depending on the content. But many articles said that wrap_content means MeasureSpec.AT_MOST. So I am confused. I don't know when MeasureSpec.AT_MOST should be applied.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The basic definition of how a View is sized goes like this:

MeasureSpec.EXACTLY - A view should be exactly this many pixels regardless of how big it actually wants to be.

MeasureSpec.AT_MOST - A view can be this size or smaller if it measures out to be smaller.

MeasureSpec.UNSPECIFIED - A view can be whatever size it needs to be in order to show the content it needs to show.

MeasureSpec.AT_MOST will be applied to views that have been set to WRAP_CONTENT if the parent view is bound in size. For example, your parent View might be bound to the screen size. It's children will be also bound to this size, but it might not be that big. Thus, the parent view will set the MeasureSpec to be AT_MOST which tells the child that it can be anywhere between 0 and screen size. The child will have to make adjustments to ensure that it fits within the bounds that was provided.

In special cases, the bounds do not matter. For example, a ScrollView. In the case of a ScrollView, the height of the child Views are irrelevant. As such, it will supply an UNSPECIFIED to the children Views which tells the children that they can be as tall as they need to be. The ScrollView will handle the drawing and placement for them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...