Android: Get the size of the Body Layout Dynamically

前端 未结 3 1525
南方客
南方客 2021-01-06 16:10

I want to get the height/width of the Middle(Body) layout in onCreate() method.

My main.xml is :



        
3条回答
  •  耶瑟儿~
    2021-01-06 16:55

    Firstly, don't get view dimensions in onCreate() since the view might not have been initialized yet. You can do it in the onStart() method. All the views will have been inflated and laid out on the screen at that point:

    @Override
        protected void onStart() {
            super.onStart();
            ScrollView scrollView = (ScrollView) findViewById(R.id.svtest);
            int width = scrollView.getWidth();
            int height = scrollView.getHeight();
        }
    

提交回复
热议问题