int[] location = new int[2]; v.getLocationOnScreen(location); int x = location[0]; int y = location[1];
import android.view.View; import android.view.ViewGroup.MarginLayoutParams; import android.widget.RelativeLayout; /* * 获取、设置控件信息 */ public class WidgetController { /* * 获取控件宽 */ public static int getWidth(View view) { int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(w, h); return (view.getMeasuredWidth()); } /* * 获取控件高 */ public static int getHeight(View view) { int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(w, h); return (view.getMeasuredHeight()); }