前言:代码很简单,网上资料很多,此篇只是做记录用。
- ViewTreeObserver
ViewTreeObserver vto = mRootView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mRootView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
mRootView.getHeight();
mRootView.getWidth();
}
});
- View.post(new Runnable())
mRootView.post(new Runnable() {
@Override
public void run() {
mRootView.getWidth();
mRootView.getHeight();
}
});