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

  • 相关阅读:
    28,intellij idea工程包含scala代码这样打包
    1、调侃程序员
    2 20个常用正则表达式
    27 Java动态加载第三方jar包中的类
    26 查看文件内容有多少行?
    25 读取jar包内log4j.properties文件方法
    MySQL表类型
    Hibernate-检索策略
    Hibernate-Native SQL
    Hibernate-Criteria Queries
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3040959.html
Copyright © 2011-2022 走看看