您的位置:首页 > 其它

onWindowFocusChanged (boolean hasWindowFocus) 一个很重要的回调函数,执行后才能获取到view的width等属性

2013-04-24 20:42 330 查看
在实现一个LinearLayout的隐藏和显示的时候,需要根据其状态来改变另一个布局的位置,这需要获取到这个LinearLayout的宽度。刚开始觉得很简单,使用LinearLayout的getMeasuredWidth就可以获取到,不过后来试了几次,获取到的值都是0。

然后我为LinearLayout注册了各种监听来执行,也都是0,然后又在Activity的onCreate、onResume等周期函数里执行,依然为0!后来上网查找相关资料,看到有人提了一个onWindowFocusChanged,去api里查了下,里面是这么解释的:

Called when the window containing this view gains or loses focus. Note that this is
separate from view focus: to receive key events, both your view and its window must have focus. If a window is displayed on top of yours that takes input focus, then your own window will lose focus but the view focus will remain unchanged.

老实说,也就看懂个大概意思,感觉没有明确提到这个特性,还希望英语好的能给个合理的翻译。后来我就去重写了这个函数,在里面获取width,然后一执行,宽度不在为0!!下面是测试的时候打印的日志~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐