Get height and width of a layout programmatically

前端 未结 16 1412
别那么骄傲
别那么骄傲 2020-11-27 13:55

I have designed an layout in which LinearLayout has 2 children LinearLayout and FrameLayout and in each child I put different views.

16条回答
  •  时光取名叫无心
    2020-11-27 14:45

    try something like this code from this link

    ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
     viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
      viewWidth = view.getWidth();
      viewHeight = view.getHeight();
    }
     });
    }
    

    hope this helps.

提交回复
热议问题