view的三種測量模式的理解

2021-07-31 22:31:09 字數 2227 閱讀 8256

measurespec是乙個32位的int型的數,表示乙個元件的大小。這個數的前2位表示測量模式,後30位表示view的尺寸大小。

view的三種測量模式如下:

measurespec.exactly:表示設定了精確的值,一般當childview設定其寬、高為精確值(例如layout_width="100dp")、match_parent時,父容器會將其設定為exactly;

measurespec.at_most:表示子布局被限制在乙個最大值內,一般當childview設定其寬、高為wrap_content時,父容器會將其設定為at_most;

measurespec.unspecified:表示子布局想要多大就多大,一般出現在aadapterview的item的heightmode中、scrollview的childview的heightmode中;此種模式比較少見。

對子view進行測量之前,會調取getchildmeasurespec方法來獲得子view的measurespec,子view的measurespec是由父容器的measurespec(parentwidthmeasurespec)還有自身的layoutparams(lp.height和lp.width),還有view自己的margin和padding來確定的,下面是從viewgroup類中找到的getchildmeasurespec的原始碼:

public static int getchildmeasurespec(int spec, int padding, int childdimension)  else if (childdimension == layoutparams.match_parent)  else if (childdimension == layoutparams.wrap_content) 

break;

// parent has imposed a maximum size on us

case measurespec.at_most:

if (childdimension >= 0) else if (childdimension == layoutparams.match_parent) else if (childdimension == layoutparams.wrap_content)

break;

// parent asked to see how big we want to be

case measurespec.unspecified:

if (childdimension >= 0) else if (childdimension == layoutparams.match_parent) else if (childdimension == layoutparams.wrap_content)

break;

}return measurespec.makemeasurespec(resultsize, resultmode);

}

以上**可用乙個**來說明:

parentmeasurespec和childmeasurespec為什麼會存在這樣的轉換關係?我是這樣理解的:

當子view指定精確的大小時,無論父容器的測量模式是什麼,父容器都會依據子view所要求的dimension來確定子view的大小,即子view的模式是exactly。

當父容器為精確模式時,父容器的大小就確定了。如果子view的屬性是match_parent,子view填滿父容器,子view的大小就等於父容器的大小,那麼子view的模式就是exactly;如果子view的屬性為wrap_content,那麼子view的大小是不確定的,但是必須小於父容器的size,所以子view的measurespec為at_most+size。

當父容器為at_most最大模式時,這是時父容器的大小不確定,但是不能大於size。此時不論子view的屬性是match_parent還是wrap_content,模式都是最大模式,並且小於size。

當父容器為unspecified未知模式時,此時無論子view的dimension為多大都是可以的,因為這個模式下父容器不限制子view的大小,要多大有多大。

MVC MVP MVVM 三種設計模式的理解

mvc model view controller mvp model view presenter mvvm model view view viewmodel mvc模式 簡介 model view controller mvc模式致力於關注點的切分,這意味著model和controller的邏...

vlan的三種模式

tag報文結構的變化是在源mac位址和目的mac位址之後,加上了4bytes的vlan資訊,也就是vlan tag頭 一般來說這樣的報文普通pc機的網絡卡是不能識別的 下圖說明了802.1q封裝tag報文幀結構 帶802.1q的幀是在標準乙太網幀上插入了4個位元組的標識。其中包含 2個位元組的協議識...

lvs lvs的三種模式

回顧了下lvs的三種模式的排程機制 1.lvs的dr模式中的arp的抑制,eth用自己口arp回應.2.keepalive是否直接操作rs?不直接操作,只操作dr 配lvs 3.tunnel模式keepalive怎麼配置?4.lvs支援的wrr,rr動態調整?dr模式圖 tunnel模式圖 三種模式...