Android getWidth和getMeasuredWidth

來源:互聯網
上載者:User

標籤:

1. 在一個類初始化時,即在建構函式當中我們是得不到View的實際大小的。感興趣的朋友可以試一下,getWidth()和getMeasuredWidth()得到的結果都是0.但是我們可以從onDraw()方法裡面的到控制項的大小。

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

getWidth(): 得到的是view在父Layout中布局好後的寬度值,如果沒有父布局,那麼預設的父布局就是整個螢幕。

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.

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

總結(正解):

 

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

getMeasuredWidth(): 對View上的內容進行測量後得到的View內容佔據的寬度,前提是你必須在父布局的onLayout()方法或者此View的onDraw()方法裡調用measure(0,0);(measure中的參數的值你自己可以定義),否則你得到的結果和getWidth()得到的結果是一樣的。

 

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

 

Android getWidth和getMeasuredWidth

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.