Android get layout height and width in a fragment

后端 未结 3 636
时光取名叫无心
时光取名叫无心 2020-12-15 05:40

I am working on a fragment and I want to get the dimension of a layout contained in the xml fragment layout. When I try the code

3条回答
  •  一个人的身影
    2020-12-15 06:13

    In onActivityCreated, the view hasn't been measured yet.

    So no, you can't get the measurements from there. onGlobalLayout is a good place to do it. Layouts happen directly after measurement.

    You can remove your listener afterwards, if you only want to do it once. Note that layouts can change size though (soft keyboards appearing, for example)

提交回复
热议问题