1. MeasureSpec.EXACTLY
The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be.
2. MeasureSpec.AT_MOST
The child can be as large as it wants up to the specified size.
3. MeasureSpec.UNSPECIFIED
The parent has not imposed any constraint on the child. It can be whatever size it wants.
4. MeasureSpec
創(chuàng)建規(guī)則
5. 調用關系
5.1 ViewGroup
調用:onMeasure
-> forEachChild
(measureChildWithMargins
-> getChildMeasureSpec
-> View
.measure
-> View
.onMeasure
)
5.2 View
調用:onMeasure
-> setMeasuredDimension
-> getDefaultSize
-> getSuggestedMinimunWidth
/getSuggestedMinimunHeight
-> determinMeasureWidthorHeight
5.3 ViewGroup
會重寫View
的onMeasure
方法柒凉,父類View
.onMeasure
基本只是通過MeasureSpec
確定大小而已,TextView
等具體view
會重寫onMeasure
方法蔑穴。
5.4 ViewGroup
會對包含的子View
進行遍歷測量跋核,具體的子View
的MeasureSpec
數(shù)據(jù)由子View
的onMeasure
方法完成叼风。
6. MeasureSpec
影響因素
父容器的MeasureSpec
和子View
的LayoutParams
共同決定了子View
的MeasureSpec
荣月。在measure
階段View
的寬和高由其measureSpec
中的specSize
決定
7. 擴展
clipToPadding
:是否讓子View
裁剪以適應父View
的padding
秃臣,默認false
clipChildren
:是否讓子View
裁剪當超過父View
的邊界,默認false
getLeft
/getX
:不要莫名其妙的將兩個不同應用場景存在的方法進行對比區(qū)分淮蜈,根據(jù)場景不就區(qū)分了嗎斋攀。getLeft
等于相對父View
的距離,getX
等于相對父View
的左邊距left
+移動TranslationX
礁芦。