使用view中的getLocationOnScreen方法,即可:
final int[] locations = new int[2];
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.getLocationOnScreen(locations);
Toast.makeText(MainActivity.this, "x is: " + locations[0], Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "y is: " + locations[1], Toast.LENGTH_SHORT).show();
}
});