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

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

问题描述

我知道为放置进行视图大小计算是不赞成的.Android 布局管理器应该正确放置所有视图.然而,当某种计算时有一些合法的情况应该完成以提供提示(例如 GridView 中的列数或调整文本视图的大小以允许在一行中显示最少数量的字符).对于这些情况,我想获取相关的视图宽度/高度信息或字体宽度/高度/等.但是 getWidth()/getHeigth()/etc 调用永远不会返回有效值除非视图已在显示器上呈现,但已经晚了.这是一个catch-22的情况.不知道有没有什么方法没找到(android api很大在我的脑海中理解所有内容)这使我上面描述的成为可能.

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.

推荐答案

在计算其父级之前无法计算视图的大小,但您始终可以通过调用 measure(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://developer.android.com/reference/android/view/View.html#measure(int, int)

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

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