Android -- onAttachedToWindow()

來源:互聯網
上載者:User

標籤:

onAttachedToWindow在Activity生命週期中的位置

放大招:

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;這行報了null 指標異常,所以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

Android -- onAttachedToWindow()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.