首页 > 代码库 > Android getMeasuredHeight()与getHeight()的区别

Android getMeasuredHeight()与getHeight()的区别

public final int getMeasuredHeight ()

Added in API level 1

Like getMeasuredHeightAndState(), but only returns the raw width component (that is the result is masked by MEASURED_SIZE_MASK).

Returns
  • The raw measured height of this view.

public final int getHeight ()

Added in API level 1

Return the height of your view.

Returns
  • The height of your view, in pixels.

getMeasuredHeight()返回的是原始测量高度,与屏幕无关,getHeight()返回的是在屏幕上显示的高度。实际上在当屏幕可以包裹内容的时候,他们的值是相等的,只有当view超出屏幕后,才能看出他们的区别。当超出屏幕后,getMeasuredHeight()等于getHeight()加上屏幕之外没有显示的高度。

 

例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="240dip"
    android:clipToPadding="false"
    android:scrollY="50dip" >

</RelativeLayout>

这里设置它的高度为240dip,超出屏幕50dip。

getMeasuredHeight()获得的就是240dip的大小。(转换为像素值)

getHeight()获得的是240dip - 50dip的大小。(转换为像素值)