【发布时间】:2014-08-17 07:13:16
【问题描述】:
在用view.measure() 测量具有恒定尺寸的View 后,getMeasuredHeight() 和getMeasureWidth() 返回 0。
layout_view.xml,为创建视图而膨胀的布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp">
</FrameLayout>
测量尺寸的函数
public void measureView(Context context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_view,null,false);
view.measure( View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Log.d(TAG,"Error width : " + view.getMeasuredWidth());
Log.d(TAG,"Error height : " + view.getMeasuredHeight());
}
【问题讨论】:
-
@GiruBhai 我尝试使用 ViewTreeObserver,它也无法正常工作
-
View.MeasureSpec.*不能直接用,下面看我的回答。