获得View的真实高度

// 标题

RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate(

R.layout.webviewheader, null);

Button back = (Button) view.findViewById(R.id.btn_back);

back.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

onBackPressed();

}

});

view.setLayoutParams(new LayoutParams(

RelativeLayout.LayoutParams.MATCH_PARENT,

RelativeLayout.LayoutParams.WRAP_CONTENT));

int w = RelativeLayout.MeasureSpec.makeMeasureSpec(0,

MeasureSpec.UNSPECIFIED);

int h = RelativeLayout.MeasureSpec.makeMeasureSpec(0,

MeasureSpec.UNSPECIFIED);

view.measure(w, h);

int x = view.getMeasuredHeight();

layout.addView(view);

如果不先设置

view.setLayoutParams(new LayoutParams(

RelativeLayout.LayoutParams.MATCH_PARENT,

RelativeLayout.LayoutParams.WRAP_CONTENT));

这句代码调用view.measure(w, h);回报空指针

posted @ 2013-08-21 15:31  天边的星星  阅读(442)  评论(0编辑  收藏  举报