Android: get width of Layout programatically having fill_parent in its xml

前端 未结 5 1775
误落风尘
误落风尘 2021-02-08 07:03

I have the a LinearLayout with width set in xml as fill_parent , now i need its width at runtime programatically. So i did this:

    Li         


        
5条回答
  •  抹茶落季
    2021-02-08 07:28

    I got a simple approach working.

    myLayout = (RelativeLayout) findViewById(R.id.my_layout);
    myLayout.post(new Runnable() 
        {
    
            @Override
            public void run()
            {
                Log.i("TEST", "Layout width : "+ myLayout.getWidth());
    
            }
        });
    

    Jens Vossnack's approach mentioned below works fine. However, I found that the onGlobalLayout() method of GlobalLayoutListener is called repeatedly, which may not be appropriate in certain cases.

提交回复
热议问题