使用 view.measure()
测量一个具有固定尺寸的 View
后,getMeasuredHeight()
和 getMeasuredWidth()
返回值为 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());
}
View.MeasureSpec.*
不能直接使用,查看下面的答案。 - M-Wajeeh