其他分享
首页 > 其他分享> > Android获取View的实际宽度和高度

Android获取View的实际宽度和高度

作者:互联网

前言:代码很简单,网上资料很多,此篇只是做记录用。

  1. 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();
            }
        });
  1. View.post(new Runnable())
mRootView.post(new Runnable() {
            @Override
            public void run() {
                mRootView.getWidth();
                mRootView.getHeight();
            }
        });

标签:VERSION,getHeight,getViewTreeObserver,mRootView,宽度,ViewTreeObserver,new,Android,
来源: https://blog.csdn.net/qq_36356379/article/details/99742016