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

前端 未结 6 576
耶瑟儿~
耶瑟儿~ 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 01:30

    @dmytrodanylyk -- I think it will return width & height as 0 so you need to use following thing

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View contentview = inflater.inflate(R.layout.YOURLAYOUTNAME, null, false);
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    int width = contentview.getMeasuredWidth();
    int height = contentview.getMeasuredHeight();
    

    It will give you right height & width.

提交回复
热议问题