How to retrieve the dimensions of a view?

前端 未结 16 1659
小蘑菇
小蘑菇 2020-11-22 01:09

I have a view made up of TableLayout, TableRow and TextView. I want it to look like a grid. I need to get the height and width of this grid. The methods

16条回答
  •  你的背包
    2020-11-22 01:23

    Height and width are zero because view has not been created by the time you are requesting it's height and width . One simplest solution is

    view.post(new Runnable() {
        @Override
        public void run() {
            view.getHeight(); //height is ready
            view.getWidth(); //width is ready
        }
    });
    

    This method is good as compared to other methods as it is short and crisp.

提交回复
热议问题