getWidth() and getHeight() always returning 0. Custom view

前端 未结 7 2008
悲哀的现实
悲哀的现实 2020-12-09 09:11

In a Fragment, I am inflating a Layout with multiple child View. I need to get the dimensions (width and height) of one of them which is a custom view.

Inside the

相关标签:
7条回答
  • 2020-12-09 09:39

    this is a real pain especially because you expected with a name like onViewCreated in fragments lifecycle that the view is ready. for me get the fragment view itself like this:

     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                super.onViewCreated(view, savedInstanceState)   
         getView()?.let{
                      it.doOnLayout{// do your UI work here }
                       }
        }
    

    this ensures the fragments getView has actually had one layout pass already.

    0 讨论(0)
提交回复
热议问题