後返回0測量View
與恆定尺寸與view.measure()
,所述getMeasuredHeight()
和getMeasureWidth()
將返回0。getmeasuredheight()和getmeasuredwidth()View.measure()
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 heeght : " + view.getMeasuredHeight());
}
看到這個http:// stackoverflow。com/questions/14592930/getwidth-returns-0-if-set-by-androidlayout-width-match-parent –
@GiruBhai我試過使用ViewTreeObserver,它也沒有工作 – SathMK
'View.MeasureSpec。*'不能使用直接在下面查看我的答案。 –