获取视图宽度/高度或字体宽/高/其它尺寸第一显示器前 [英] Get view width/height or font width/height/other sizes before first display

查看:130
本文介绍了获取视图宽度/高度或字体宽/高/其它尺寸第一显示器前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这样做视图的大小计算位置的眉头皱了起来。 Android的布局管理器应妥善放置所有的意见。 但也有一些合理的情况下,当某种计算 应以提供提示(例如来完成。列数在GridView 或上浆文本视图,以允许在一条线上显示的字符的最小数量)。 对于这些情况下,我想获得相关的视图宽度/高度信息或 字体宽/高/等。 然而,的getWidth()/ getHeigth()/等调用永远不会返回一个有效的值 除非视图已经被渲染上展出,但随后就晚了。 这是一个左右为难的局面。 我不知道是否有,我还没有找到某种方式(Android的API是大 对COM prehend都在我的脑海里),使我上面可能有描述。

I know that doing view size calculations for placement is frowned up. Android layout managers should place all views properly. However there are some legitimate cases when some sort of calculations should be done in order to provide hints (ex. number of columns in a GridView or sizing a text view to allow a minimum number of characters displayed in a line). For these cases I would like to get the relevant view width/height info or font width/height/etc. However the getWidth()/getHeigth()/etc calls never return a valid value unless the view has been rendered on display but then it is late. It is a catch-22 situation. I wonder if there is some way that I haven't found (the android api is big to comprehend all in my mind) that makes what I have described above possible.

推荐答案

尺寸来看是无法计算,直到它的母公司进行计算,但您随时可以通过调用措施(allowedWidth,allowedHeight迫使该计算)就可以了。然后 getMeasuredWidth()将作品。

The size of view cannot be calculated until its parent is calculated, but you can always force this calculation by calling measure(allowedWidth, allowedHeight) on it. Then getMeasuredWidth() will works.

的http://开发商。 android.com/reference/android/view/View.html#measure(int,INT)

这篇关于获取视图宽度/高度或字体宽/高/其它尺寸第一显示器前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆