给定TextView
,是否可以在运行时知道绘制位置的X和Y坐标?
是否也可以知道像素的大小(宽度/长度)?
答案 0 :(得分:14)
视图有getLeft()
,getTop()
,getWidth()
,getHeight()
方法,也适用于textView。有关更多信息,请参阅以下链接...
getLeft()
和getTop()
将返回起始x,y坐标。
http://developer.android.com/reference/android/view/View.html
答案 1 :(得分:2)
相对于父级的坐标
int x = textView.getLeft();
int y = textView.getTop();
绝对坐标
int[] location = new int[2];
textView.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
有关详情,请参阅this answer。
像素大小
int width = textView.getWidth();
int height = textView.getHeight();
备注强>
(0,0)
,可能是因为您获得了与父布局相关的相对坐标(并且它位于父级的左上角)。也可能是因为您试图在视图布局之前获取坐标(例如,在onCreate()
中)。