Android -- onAttachedToWindow()_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android -- onAttachedToWindow()

Android -- onAttachedToWindow()

 2015/5/5 9:43:55  我爱物联网  程序员俱乐部  我要评论(0)
  • 摘要:onAttachedToWindow在Activity生命周期中的位置放大招:onAttachedToWindow不能在onCreate中哟~Viewview=getWindow().getDecorView();WindowManager.LayoutParamslp=(WindowManager.LayoutParams)view.getLayoutParams();lp.gravity=Gravity.CENTER;lp.width=(dm.widthPixels*4)/5;lp
  • 标签:android

onAttachedToWindow在Activity生命周期中的位置

放大招:

http_staticfree.info_~steve_complete_android_fragment_lifecycle.svg

onAttachedToWindow不能在onCreate中哟~

View view = getWindow().getDecorView();  
WindowManager.LayoutParams lp = (WindowManager.LayoutParams)view.getLayoutParams();  
lp.gravity = Gravity.CENTER;  
lp.width = (dm.widthPixels * 4) / 5;  
lp.height = (dm.widthPixels * 4) / 5;  
getWindowManager().updateViewLayout(view,lp);

放到onCreate中进行测试,结果在lp.gravity = Gravity.CENTER;这行报了空指针异常,所以view.getLayoutParams()获取的LayoutParams是空。

public final class ActivityThread {    
    ......    
    
    final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {    
        ......    
    
        ActivityClientRecord r = performResumeActivity(token, clearHide);    
    
        if (r != null) {    
            final Activity a = r.activity;    
            ......    
    
            // If the window hasn't yet been added to the window manager,    
            // and this guy didn't finish itself or start another activity,    
            // then go ahead and add the window.    
            boolean willBeVisible = !a.mStartedActivity;    
            if (!willBeVisible) {    
                try {    
                    willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(    
                            a.getActivityToken());    
                } catch (RemoteException e) {    
                }    
            }    
            if (r.window == null && !a.mFinished && willBeVisible) {    
                r.window = r.activity.getWindow();    
                View decor = r.window.getDecorView();    
                decor.setVisibility(View.INVISIBLE);    
                ViewManager wm = a.getWindowManager();    
                WindowManager.LayoutParams l = r.window.getAttributes();    
                a.mDecor = decor;    
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;    
                ......    
                if (a.mVisibleFromClient) {    
                    a.mWindowAdded = true;    
                    wm.addView(decor, l);    
                }    
            }     
    
            ......    
        }    
    
        ......    
    }    
      
    ......    
}

原来在ActivityThread执行handleResumeActivity时就会为PhoneWindow(r.activity.getWindow)中的DecorView设置LayoutParam,并且通过源码发现handleResumeActivity函数首先会执行performResumeActivity,此时会调用Activity的onResume()生命周期函数,这时问题就比较清晰了,看来只要在Activity的onResume生命周期后就能获取DecorView的LayoutParam,进而可以设置高度和宽度了。根据上面贴出的生命周期图,onResume()后面是onAttachedToWindow(),并且onAttachedToWindow只会调用一次,不会受用户操作行为影响。所以在onAttachedToWindow中进行窗口尺寸的修改再合适不过了。

干货

  • onAttachedToWindow运行在onResume之后;
  • DecorView的LayoutParams是在ActivityThread的handleResumeActivity中设置的,并且该函数会调用Activity的onResume生命周期,所以在onResume之后可以设置窗体尺寸;

我是天王盖地虎的分割线

 

 

参考:http://blog.csdn.net/guxiao1201/article/details/41517871

发表评论
用户名: 匿名