How to get programmatically width and height of Relative - Linear layout in android?

前端 未结 6 1652
礼貌的吻别
礼貌的吻别 2020-12-11 01:53

I required runtime dimensions of Relative/Linear Layout.

activity_home.xml:



        
6条回答
  •  天涯浪人
    2020-12-11 02:35

    Well I have implemented by this way:

    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout);
    ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); 
    viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            int width  = linearLayout.getMeasuredWidth();
            int height = linearLayout.getMeasuredHeight(); 
    
        } 
    });
    

    This example is for Linear layout, for Relative layout follow same process.

    Hope this would help you.

提交回复
热议问题