Relative Layout get Width/Height return 0

感情迁移 提交于 2019-11-28 04:19:48

问题


I need to get width and height of Relative layout, however I do not know when the Relative Layout is ready to provide me these two attributes. My activity has several tabs and I call the getWidth() and getHeight() in onCreateView() after inflater.inflate(R.layout.fragment_pizza, container, false);, however it returns 0 for both of them, so they are not apparently ready. Is there some kind of event like afterCreateView() or something similar where I can call these two methods and get the actual width and height?

Code:

public class PizzaFragment extends Fragment {

    private static final String TAG = "PizzaFragment";

    private Controller controller;

    private static final int BUTTON_WIDTH = 70;
    private static final int BUTTON_HEIGHT = 20;

    private static final int MARGIN_LEFT = 80;
    private static final int MARGIN_BOTTOM = 30;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        controller = Controller.getInstance();
        controller.loadProducts("pizza", getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pizza, container, false);

        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.rl_order_pizza);
        Log.d(TAG, "Width: " + layout.getWidth());
        Log.d(TAG, "Height: " + layout.getHeight());

        int currentX = 10;
        int currentY = 10;

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(BUTTON_WIDTH, BUTTON_HEIGHT);


        for (Product product: controller.getProducts("pizza")){

            Button tempButton = new Button(getActivity());
            tempButton.setId(product.getId());
            tempButton.setText(product.getName());

            if (layout.getWidth() < currentX + MARGIN_LEFT){
                currentX = 10;
                currentY = MARGIN_BOTTOM;

            }
            else{
                currentX += MARGIN_LEFT;
            }

            Log.d(TAG, "CurrentY: " + currentY);
            Log.d(TAG, "CurrentX: " + currentX);

            layoutParams.leftMargin = currentX;
            layoutParams.bottomMargin = currentY;

            tempButton.setLayoutParams(layoutParams);

            layout.addView(tempButton);
        }

        return view;

    }
}

回答1:


My suggestion is to use a global layout listener like this in onCreateView:

YOUR_LAYOUT_INSTANCE = view.findViewById(R.id.YOUR_LAYOUT_ID);

...

YOUR_LAYOUT_INSTANCE.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
    @Override
    public void onGlobalLayout()
    {
        // gets called after layout has been done but before display.
        if (Build.VERSION.SDK_INT < 16) {
            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        // get width and height
    }
});



回答2:


addOnGlobalLayoutListener is deprecated use removeOnGlobalLayoutListener .For better handling extending the solution provided above by @fasteque

    YOUR_LAYOUT_INSTANCE.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            // gets called after layout has been done but before display.
            if (Build.VERSION.SDK_INT < 16) {

                        YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } 
            else {
                        YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }

        // get width and height
        }
    });


来源:https://stackoverflow.com/questions/19413268/relative-layout-get-width-height-return-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!