How to find the Width of the a view before the view is displayed?

前端 未结 6 577
耶瑟儿~
耶瑟儿~ 2020-12-14 01:07

How to find the Width of the a view before the view is displayed? I do not want to add a custom view and tap the dimensions in the OnChanged().

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 01:14

    I like to use this technique as per my answer on a related question:

    Post a runnable from onCreate() that will be executed when the view has been created:

        contentView = findViewById(android.R.id.content);
        contentView.post(new Runnable()
        {
            public void run()
            {
                contentHeight = contentView.getHeight();
            }
        });
    

    This code will run on the main UI thread, after onCreate() has finished.

提交回复
热议问题