Android getWidth和getMeasuredWidth

1. 在一個類初始化時,即在構造函數當中咱們是得不到View的實際大小的。感興趣的朋友能夠試一下,getWidth()和getMeasuredWidth()獲得的結果都是0.可是咱們能夠從onDraw()方法裏面的到控件的大小。函數

2.這兩個所獲得的結果的單位是像素即pixel。佈局

getWidth(): 獲得的是view在父Layout中佈局好後的寬度值,若是沒有父佈局,那麼默認的父佈局就是整個屏幕。this

getMeasuredWidth():先看一下API裏面是怎麼說的。
The width of this view as measured in the most recent call to measure(). This should be used during measurement and layout calculations only..net

獲得的是最近一次調用measure()方法測量後獲得的是View的寬度,它僅僅用在測量和Layout的計算中。因此此方法獲得的是View的內容佔據的實際寬度。blog

 

總結(正解): rem

getWidth(): View在設定好佈局後整個View的寬度。get

getMeasuredWidth(): 對View上的內容進行測量後獲得的View內容佔據的寬度,前提是你必須在父佈局的onLayout()方法或者此View的onDraw()方法裏調用measure(0,0);(measure中的參數的值你本身能夠定義),不然你獲得的結果和getWidth()獲得的結果是同樣的。io

 

轉自:http://blog.csdn.net/wangbofei/article/details/7795430sed

相關文章
相關標籤/搜索