如何获取TextView的XY坐标和像素大小?
给定一个 TextView,是否可以在运行时知道其绘制位置的 X 和 Y 坐标? 是否还可以知道以像素为单位的尺寸(宽度/长度)?
Given a TextView
, is it possible to know at runtime the X and Y coordinates of where it is drawn?
Is it also possible to know the size (width/length) in pixels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图有
getLeft()
、getTop()
、getWidth()
、getHeight()
方法,它也适用于 textView。有关详细信息,请参阅以下链接...getLeft()
和getTop()
将返回起始 x,y 坐标。http://developer.android.com/reference/android/view/View.html
There are
getLeft()
,getTop()
,getWidth()
,getHeight()
methods for a view, it works for textView too. for more information , see the following link...getLeft()
andgetTop()
will return you the starting x,y co-ordinates.http://developer.android.com/reference/android/view/View.html
相对于父级的坐标
绝对坐标
有关更多信息,请参阅此答案。
像素大小
注释
(0,0)
,可能是因为您正在获取与父布局相关的相对坐标(并且它位于父级的左上角)。也可能是因为您试图在视图布局之前获取坐标(例如,在onCreate()
中)。Coordinates relative to parent
Absolute coordinates
See this answer for more.
Pixel size
Notes
(0,0)
it could be because you are getting the relative coordinates related to the parent layout (and it is sitting in the top left corner of the parent). It could also be because you are trying to get the coordinates before the view has been laid out (for example, inonCreate()
).