Getting 0 Value for both X and Y Coordinate of view(Imagebutton) in android

前端 未结 4 1413
失恋的感觉
失恋的感觉 2021-01-01 05:46

I want to get X and Y Point of View(ImageButton).

When I try to find X and Y on Click event with below code I get proper X and Y coordinate for View.



        
4条回答
  •  情话喂你
    2021-01-01 06:14

    post() gets called after setContentView().

    Method setContentView() ends up in callingViewGroup.addView() of the top view, andaddView() call always triggers requestLayout(). In turn, requestLayout() posts a task to the main thread to be executed later. This task will execute measure and layout on the view hierarchy. Now if you post another task it will be put into the queue afterlayout task and, as the result, always executed aftermeasure and layout happen. Thus you will always have valid sizes.

    Taken from https://stackoverflow.com/a/21938380

    button.post(new Runnable() {
        @Override
        public void run() {
            // get coordinates
        }
    });
    

提交回复
热议问题