PhoneWindow.generateLayout方法

 protected ViewGroup generateLayout(DecorView decor) {
3335        // Apply data from current theme.
3336
3337        TypedArray a = getWindowStyle();  // 这里获取窗口的样式
3338
3339        if (false) {
3340            System.out.println("From style:");
3341            String s = "Attrs:";
3342            for (int i = 0; i < R.styleable.Window.length; i++) {
3343                s = s + " " + Integer.toHexString(R.styleable.Window[i]) + "="
3344                        + a.getString(i);
3345            }
3346            System.out.println(s);
3347        }
3348
3349        mIsFloating = a.getBoolean(R.styleable.Window_windowIsFloating, false);  // 是否悬浮
3350        int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR)
3351                & (~getForcedWindowFlags());
3352        if (mIsFloating) {
3353            setLayout(WRAP_CONTENT, WRAP_CONTENT);
3354            setFlags(0, flagsToUpdate);
3355        } else {
3356            setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate);
3357        }
3358
3359        if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
3360            requestFeature(FEATURE_NO_TITLE);
3361        } else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) {
3362            // Don't allow an action bar if there is no title.
3363            requestFeature(FEATURE_ACTION_BAR);
3364        }
3365
3366        if (a.getBoolean(R.styleable.Window_windowActionBarOverlay, false)) {
3367            requestFeature(FEATURE_ACTION_BAR_OVERLAY);
3368        }
3369
3370        if (a.getBoolean(R.styleable.Window_windowActionModeOverlay, false)) {
3371            requestFeature(FEATURE_ACTION_MODE_OVERLAY);
3372        }
3373
3374        if (a.getBoolean(R.styleable.Window_windowSwipeToDismiss, false)) {
3375            requestFeature(FEATURE_SWIPE_TO_DISMISS);
3376        }
3377
3378        if (a.getBoolean(R.styleable.Window_windowFullscreen, false)) {
3379            setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN & (~getForcedWindowFlags()));
3380        }
3381
3382        if (a.getBoolean(R.styleable.Window_windowTranslucentStatus,
3383                false)) {
3384            setFlags(FLAG_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS
3385                    & (~getForcedWindowFlags()));
3386        }
3387
3388        if (a.getBoolean(R.styleable.Window_windowTranslucentNavigation,
3389                false)) {
3390            setFlags(FLAG_TRANSLUCENT_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION
3391                    & (~getForcedWindowFlags()));
3392        }
3393
3394        if (a.getBoolean(R.styleable.Window_windowOverscan, false)) {
3395            setFlags(FLAG_LAYOUT_IN_OVERSCAN, FLAG_LAYOUT_IN_OVERSCAN&(~getForcedWindowFlags()));
3396        }
3397
3398        if (a.getBoolean(R.styleable.Window_windowShowWallpaper, false)) {
3399            setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags()));
3400        }
3401
3402        if (a.getBoolean(R.styleable.Window_windowEnableSplitTouch,
3403                getContext().getApplicationInfo().targetSdkVersion
3404                        >= android.os.Build.VERSION_CODES.HONEYCOMB)) {
3405            setFlags(FLAG_SPLIT_TOUCH, FLAG_SPLIT_TOUCH&(~getForcedWindowFlags()));
3406        }
3407
3408        a.getValue(R.styleable.Window_windowMinWidthMajor, mMinWidthMajor);
3409        a.getValue(R.styleable.Window_windowMinWidthMinor, mMinWidthMinor);
3410        if (a.hasValue(R.styleable.Window_windowFixedWidthMajor)) {
3411            if (mFixedWidthMajor == null) mFixedWidthMajor = new TypedValue();
3412            a.getValue(R.styleable.Window_windowFixedWidthMajor,
3413                    mFixedWidthMajor);
3414        }
3415        if (a.hasValue(R.styleable.Window_windowFixedWidthMinor)) {
3416            if (mFixedWidthMinor == null) mFixedWidthMinor = new TypedValue();
3417            a.getValue(R.styleable.Window_windowFixedWidthMinor,
3418                    mFixedWidthMinor);
3419        }
3420        if (a.hasValue(R.styleable.Window_windowFixedHeightMajor)) {
3421            if (mFixedHeightMajor == null) mFixedHeightMajor = new TypedValue();
3422            a.getValue(R.styleable.Window_windowFixedHeightMajor,
3423                    mFixedHeightMajor);
3424        }
3425        if (a.hasValue(R.styleable.Window_windowFixedHeightMinor)) {
3426            if (mFixedHeightMinor == null) mFixedHeightMinor = new TypedValue();
3427            a.getValue(R.styleable.Window_windowFixedHeightMinor,
3428                    mFixedHeightMinor);
3429        }
3430        if (a.getBoolean(R.styleable.Window_windowContentTransitions, false)) {
3431            requestFeature(FEATURE_CONTENT_TRANSITIONS);
3432        }
3433        if (a.getBoolean(R.styleable.Window_windowActivityTransitions, false)) {
3434            requestFeature(FEATURE_ACTIVITY_TRANSITIONS);
3435        }
3436
3437        final WindowManager windowService = (WindowManager) getContext().getSystemService(
3438                Context.WINDOW_SERVICE);
3439        if (windowService != null) {
3440            final Display display = windowService.getDefaultDisplay();
3441            final boolean shouldUseBottomOutset =
3442                    display.getDisplayId() == Display.DEFAULT_DISPLAY
3443                            || (getForcedWindowFlags() & FLAG_FULLSCREEN) != 0;
3444            if (shouldUseBottomOutset && a.hasValue(R.styleable.Window_windowOutsetBottom)) {
3445                if (mOutsetBottom == null) mOutsetBottom = new TypedValue();
3446                a.getValue(R.styleable.Window_windowOutsetBottom,
3447                        mOutsetBottom);
3448            }
3449        }
3450
3451        final Context context = getContext();
3452        final int targetSdk = context.getApplicationInfo().targetSdkVersion;
3453        final boolean targetPreHoneycomb = targetSdk < android.os.Build.VERSION_CODES.HONEYCOMB;
3454        final boolean targetPreIcs = targetSdk < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
3455        final boolean targetPreL = targetSdk < android.os.Build.VERSION_CODES.LOLLIPOP;
3456        final boolean targetHcNeedsOptions = context.getResources().getBoolean(
3457                R.bool.target_honeycomb_needs_options_menu);
3458        final boolean noActionBar = !hasFeature(FEATURE_ACTION_BAR) || hasFeature(FEATURE_NO_TITLE);
3459
3460        if (targetPreHoneycomb || (targetPreIcs && targetHcNeedsOptions && noActionBar)) {
3461            setNeedsMenuKey(WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE);
3462        } else {
3463            setNeedsMenuKey(WindowManager.LayoutParams.NEEDS_MENU_SET_FALSE);
3464        }
3465
3466        // Non-floating windows on high end devices must put up decor beneath the system bars and
3467        // therefore must know about visibility changes of those.
3468        if (!mIsFloating && ActivityManager.isHighEndGfx()) {
3469            if (!targetPreL && a.getBoolean(
3470                    R.styleable.Window_windowDrawsSystemBarBackgrounds,
3471                    false)) {
3472                setFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
3473                        FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS & ~getForcedWindowFlags());
3474            }
3475        }
3476        if (!mForcedStatusBarColor) {
3477            mStatusBarColor = a.getColor(R.styleable.Window_statusBarColor, 0xFF000000);
3478        }
3479        if (!mForcedNavigationBarColor) {
3480            mNavigationBarColor = a.getColor(R.styleable.Window_navigationBarColor, 0xFF000000);
3481        }
3482
3483        if (mAlwaysReadCloseOnTouchAttr || getContext().getApplicationInfo().targetSdkVersion
3484                >= android.os.Build.VERSION_CODES.HONEYCOMB) {
3485            if (a.getBoolean(
3486                    R.styleable.Window_windowCloseOnTouchOutside,
3487                    false)) {
3488                setCloseOnTouchOutsideIfNotSet(true);
3489            }
3490        }
3491
3492        WindowManager.LayoutParams params = getAttributes();
3493
3494        if (!hasSoftInputMode()) {
3495            params.softInputMode = a.getInt(
3496                    R.styleable.Window_windowSoftInputMode,
3497                    params.softInputMode);
3498        }
3499
3500        if (a.getBoolean(R.styleable.Window_backgroundDimEnabled,
3501                mIsFloating)) {
3502            /* All dialogs should have the window dimmed */
3503            if ((getForcedWindowFlags()&WindowManager.LayoutParams.FLAG_DIM_BEHIND) == 0) {
3504                params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
3505            }
3506            if (!haveDimAmount()) {
3507                params.dimAmount = a.getFloat(
3508                        android.R.styleable.Window_backgroundDimAmount, 0.5f);
3509            }
3510        }
3511
3512        if (params.windowAnimations == 0) {
3513            params.windowAnimations = a.getResourceId(
3514                    R.styleable.Window_windowAnimationStyle, 0);
3515        }
3516
3517        // The rest are only done if this window is not embedded; otherwise,
3518        // the values are inherited from our container.
3519        if (getContainer() == null) {
3520            if (mBackgroundDrawable == null) {
3521                if (mBackgroundResource == 0) {
3522                    mBackgroundResource = a.getResourceId(
3523                            R.styleable.Window_windowBackground, 0);
3524                }
3525                if (mFrameResource == 0) {
3526                    mFrameResource = a.getResourceId(R.styleable.Window_windowFrame, 0);
3527                }
3528                mBackgroundFallbackResource = a.getResourceId(
3529                        R.styleable.Window_windowBackgroundFallback, 0);
3530                if (false) {
3531                    System.out.println("Background: "
3532                            + Integer.toHexString(mBackgroundResource) + " Frame: "
3533                            + Integer.toHexString(mFrameResource));
3534                }
3535            }
3536            mElevation = a.getDimension(R.styleable.Window_windowElevation, 0);
3537            mClipToOutline = a.getBoolean(R.styleable.Window_windowClipToOutline, false);
3538            mTextColor = a.getColor(R.styleable.Window_textColor, Color.TRANSPARENT);
3539        }
3540
3541        // Inflate the window decor.
3542
3543        int layoutResource;
3544        int features = getLocalFeatures();
3545        // System.out.println("Features: 0x" + Integer.toHexString(features));
3546        if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
3547            layoutResource = R.layout.screen_swipe_dismiss;
3548        } else if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
3549            if (mIsFloating) {
3550                TypedValue res = new TypedValue();
3551                getContext().getTheme().resolveAttribute(
3552                        R.attr.dialogTitleIconsDecorLayout, res, true);
3553                layoutResource = res.resourceId;
3554            } else {
3555                layoutResource = R.layout.screen_title_icons;
3556            }
3557            // XXX Remove this once action bar supports these features.
3558            removeFeature(FEATURE_ACTION_BAR);
3559            // System.out.println("Title Icons!");
3560        } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0
3561                && (features & (1 << FEATURE_ACTION_BAR)) == 0) {
3562            // Special case for a window with only a progress bar (and title).
3563            // XXX Need to have a no-title version of embedded windows.
3564            layoutResource = R.layout.screen_progress;
3565            // System.out.println("Progress!");
3566        } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
3567            // Special case for a window with a custom title.
3568            // If the window is floating, we need a dialog layout
3569            if (mIsFloating) {
3570                TypedValue res = new TypedValue();
3571                getContext().getTheme().resolveAttribute(
3572                        R.attr.dialogCustomTitleDecorLayout, res, true);
3573                layoutResource = res.resourceId;
3574            } else {
3575                layoutResource = R.layout.screen_custom_title;
3576            }
3577            // XXX Remove this once action bar supports these features.
3578            removeFeature(FEATURE_ACTION_BAR);
3579        } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
3580            // If no other features and not embedded, only need a title.
3581            // If the window is floating, we need a dialog layout
3582            if (mIsFloating) {
3583                TypedValue res = new TypedValue();
3584                getContext().getTheme().resolveAttribute(
3585                        R.attr.dialogTitleDecorLayout, res, true);
3586                layoutResource = res.resourceId;
3587            } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) {
3588                layoutResource = a.getResourceId(
3589                        R.styleable.Window_windowActionBarFullscreenDecorLayout,
3590                        R.layout.screen_action_bar);
3591            } else {
3592                layoutResource = R.layout.screen_title;
3593            }
3594            // System.out.println("Title!");
3595        } else if ((features & (1 << FEATURE_ACTION_MODE_OVERLAY)) != 0) {
3596            layoutResource = R.layout.screen_simple_overlay_action_mode;
3597        } else {
3598            // Embedded, so no decoration is needed.
3599            layoutResource = R.layout.screen_simple;
3600            // System.out.println("Simple!");
3601        }
3602
3603        mDecor.startChanging();
3604
3605        View in = mLayoutInflater.inflate(layoutResource, null);
3606        decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
3607        mContentRoot = (ViewGroup) in;
3608
3609        ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
3610        if (contentParent == null) {
3611            throw new RuntimeException("Window couldn't find content container view");
3612        }
3613
3614        if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
3615            ProgressBar progress = getCircularProgressBar(false);
3616            if (progress != null) {
3617                progress.setIndeterminate(true);
3618            }
3619        }
3620
3621        if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
3622            registerSwipeCallbacks();
3623        }
3624
3625        // Remaining setup -- of background and title -- that only applies
3626        // to top-level windows.
3627        if (getContainer() == null) {
3628            final Drawable background;
3629            if (mBackgroundResource != 0) {
3630                background = getContext().getDrawable(mBackgroundResource);
3631            } else {
3632                background = mBackgroundDrawable;
3633            }
3634            mDecor.setWindowBackground(background);
3635
3636            final Drawable frame;
3637            if (mFrameResource != 0) {
3638                frame = getContext().getDrawable(mFrameResource);
3639            } else {
3640                frame = null;
3641            }
3642            mDecor.setWindowFrame(frame);
3643
3644            mDecor.setElevation(mElevation);
3645            mDecor.setClipToOutline(mClipToOutline);
3646
3647            if (mTitle != null) {
3648                setTitle(mTitle);
3649            }
3650
3651            if (mTitleColor == 0) {
3652                mTitleColor = mTextColor;
3653            }
3654            setTitleColor(mTitleColor);
3655        }
3656
3657        mDecor.finishChanging();
3658
3659        return contentParent;
3660    }

// 设置窗口的宽高

673     public void setLayout(int width, int height) {
674         final WindowManager.LayoutParams attrs = getAttributes();
675         attrs.width = width;
676         attrs.height = height;
677         dispatchWindowAttributesChanged(attrs);
678     }
原文地址:https://www.cnblogs.com/huyang011/p/7499483.html