1,
tvtest.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, tvtest.getWidth() +"--" +tvtest.getHeight(), Toast.LENGTH_SHORT).show();
}
});
2,
ViewTreeObservervto2 = ll_sticky_header.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
heightHeader = ll_sticky_header.getMeasuredHeight();
}});
View.post()中的run方法和onWindowFocusChanged方法几乎是同时执行,都是在onLayout之后,所以都是可以得到控件的宽高的!!!
3,
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mWidth = mTextView.getWidth();
mHeight = mTextView.getHeight();
Log.d(TAG, "onWindowFocusChanged: width = " + mWidth + " height = " + mHeight);
}
oncreate()→onResume()→onMeasure()→onLayout()→onWidnowFocusChanged()→.....→onDraw()...
4,自定义view
onMeasure()完成才能通过getMeasuredHeight()和getMeasuredWidth()获取宽高
onLayout()完成才能通过getHeight()和getWidth()获取宽高
网友评论