获取view宽高的2种方式(getMeasuredHeight和getHeight区别)

获取view宽高的2中方式

  • 1.getMeasuredHeight:必须在onMeasure方法执行之后才能获取到宽高

       做法:主动测量,调用view.measure(0,0);


  • 2.getHeight:必须在onLayout方法执行之后才能获取到

      做法:

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                ViewPropertyAnimator.animate(view).translationY(view.getHeight()).setDuration(0);
            }
        });




你可能感兴趣的:(区别,getHeight,获取view宽高)