Android TextView has height and width of 0

后端 未结 4 1321
野性不改
野性不改 2020-12-08 02:33

I have a small project where I want to reuse a certain UI component a few time so I created a widget by expanding a ViewGroup. In that ViewGroup I inflated a view that conta

4条回答
  •  醉梦人生
    2020-12-08 03:13

    Remember:getHeight() and getWidth()return 0 if components are not drawn yet.


    To find the width And height of a View before it being drawn:

    1. First call measure

      view.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)
      
    2. Now you can get width using getMeasuredWidth and height using getMeasuredHeight

      int width = view.getMeasuredWidth();
      int height = view.getMeasuredHeight();
      

    I have posted some more ideas here: How to get width/height of a View

提交回复
热议问题