How to find the Width of the a view before the view is displayed?

前端 未结 6 574
耶瑟儿~
耶瑟儿~ 2020-12-14 01:07

How to find the Width of the a view before the view is displayed? I do not want to add a custom view and tap the dimensions in the OnChanged().

6条回答
  •  悲&欢浪女
    2020-12-14 01:30

    You should use OnGlobalLayoutListener which is called for changes on the view, but before it is drawn.

    costumView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
        @Override
        public void onGlobalLayout() {
    
          costumView.getWidth();
    
        }
    });
    

提交回复
热议问题