现在的位置: 首页 > 综合 > 正文

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

2013年03月09日 ⁄ 综合 ⁄ 共 633字 ⁄ 字号 评论关闭

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

  然后我为LinearLayout注册了各种监听来执行,也都是0,然后又在Activity的oncCreate、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!!下面是测试的时候打印的日志~

抱歉!评论已关闭.