问题
How do I listen on a View to obtain its XY coordination if it is moving or its XY position change? The View is not necessary to be dragged (or by user touch event), it may change if the orientation change or pushed (upward) by the showing keyboard or etc. I want to know its updated XY coordination when it changes, how can I do that?
回答1:
You may do something like this
final View activityRootView = findViewById(R.id.mainLayout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
findViewById(R.id.textView).getX();
findViewById(R.id.textView).getY();
}
});
Hope this helps.
来源:https://stackoverflow.com/questions/20461784/listen-on-a-view-if-the-view-change-its-xy-position