When should I get Width View in Fragment

前端 未结 2 1739
一向
一向 2021-02-08 21:36

I adding View (button) programally in Linearlayout.LinearLayout is layouted by XML in Fragment.

I want to get button width, but always return 0.

I googled thi

2条回答
  •  迷失自我
    2021-02-08 21:54

    I'd had a similar problem and solved it in the Fragment callback onViewCreated() like this:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.post(new Runnable() {
            @Override
            public void run() {
                // do operations or methods involved
                // View.getWidth(); or View.getHeight();
                // here
            }
        });
    }
    

    run() runs after all views were rendered...

提交回复
热议问题