如何知道字符串占用的像素数?
我有一个项目(TextField 或 TextArea,...)。它具有内容价值,说“你好世界”。如何知道这个“hello world”字符串值在屏幕上占用的像素数?
I have an item ( TextField, or TextArea, ...). It has a content value , say "hello world". How to know the number of pixels that this "hello world" String value is occupying on the screen ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以获得首选宽度/高度以进行粗略估计(您还需要添加边距以获得准确的尺寸)。然而,布局管理器将这些视为指导方针,而不是最终尺寸,并且可以决定将组件放置在任何地方。
在运行时(例如绘制等)期间,getX/Y/Width/Height 参数提供准确的组件尺寸和位置。但是,这些仅对当前绘制操作有效,因为设备可能会旋转或可能需要布局回流。
您需要更具体地说明您想要实现的目标。
You can get the preferred width/height to get a rough estimation (you would also need to add the margin's to get accurate sizing). However the layout manager views these as guidelines not as final sizes and can decide on placing a component anywhere.
During runtime e.g. paint etc. the getX/Y/Width/Height argument provide accurate component size and position. However, these are only valid for the current paint operation since a device may be rotated or might require layout reflow.
You need to be more specific on what you are trying to accomplish.