zoukankan      html  css  js  c++  java
  • onWindowFocusChanged (boolean hasWindowFocus) 一个很重要的回调函数,执行后才能获取到view的width等属性

      在实现一个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!!下面是测试的时候打印的日志~

  • 相关阅读:
    16-高级指针
    15-C语言结构体
    14-C语言宏
    13-C语言字符串函数库
    12-C语言字符串
    11-C语言指针
    10-C语言函数
    POJ 1001 高精度乘法
    POJ 1060 多项式乘法和除法取余
    POJ 1318 字典排序
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3040959.html
Copyright © 2011-2022 走看看