Android dump之解決狀態(tài)欄顏色不生效問題

一、問題描述

由于我們的項(xiàng)目是手機(jī)桌面的負(fù)一屏,沒有Activity而是通過Window.addView實(shí)現(xiàn)的。
所以無法直接使用常規(guī)的設(shè)置狀態(tài)欄的方式修改顏色。
最后通過分析原理,給View設(shè)置標(biāo)志位,結(jié)果還有問題,就有了這一篇文章了。

二、排查流程

一圖勝千言


狀態(tài)欄顏色修改不生效問題流程排查.png
  1. 場(chǎng)景
    沉浸式,一個(gè)動(dòng)畫滾動(dòng)的場(chǎng)景下需要吸頂,然后對(duì)應(yīng)的狀態(tài)欄文字需要跟著變換顏色。
  2. 情況特殊
    由于沒有Activity的存在,無法直接調(diào)用Activity的方法實(shí)現(xiàn)修改狀態(tài)欄
  3. 分析狀態(tài)欄修改原理 參考
    其實(shí)就是給view設(shè)置標(biāo)志位,與Activity并沒有關(guān)系
  4. 分析誰調(diào)用了這個(gè)標(biāo)識(shí) Appearance_Light_Status_Bars
    找到了是DisplayPolicy
  5. 通過dump逐一排查當(dāng)前狀態(tài)
    5.1. 肯定是appWindow,因?yàn)橛玫木褪荳indowManager.LayoutParams.TYPE_APPLICATION
    5.2. attached應(yīng)該為null,因?yàn)椴皇莝ubWindow
    5.3. 發(fā)現(xiàn)少了FullScreen標(biāo)識(shí),一般正常的Activity都有這個(gè)標(biāo)識(shí)。但我們是負(fù)一屏,沒有Activity,是通過window添加的,那么這個(gè)添加的參數(shù)是否有問題。
  6. 查詢isFullScreen方法的判斷標(biāo)準(zhǔn)
    果然問題就在這里,我們?cè)仍O(shè)置的寬高直接讀取了屏幕高寬,并非設(shè)置的MatchParent

三、相應(yīng)的源碼

DisplayPolicy

    /**
     * Called following layout of all window to apply policy to each window.
     *
     * @param win The window being positioned.
     * @param attrs The LayoutParams of the window.
     * @param attached For sub-windows, the window it is attached to. Otherwise null.
     */
    public void applyPostLayoutPolicyLw(WindowState win, WindowManager.LayoutParams attrs,
            WindowState attached, WindowState imeTarget) {
    //省略若干...
        boolean appWindow = attrs.type >= FIRST_APPLICATION_WINDOW
                && attrs.type < FIRST_SYSTEM_WINDOW;
    //省略若干...
        // Check the windows that overlap with system bars to determine system bars' appearance.
        if ((appWindow && attached == null && attrs.isFullscreen())
                || attrs.type == TYPE_VOICE_INTERACTION) {
                //省略若干...
            // Cache app windows that is overlapping with the status bar to determine appearance
            // of status bar.
            if (mStatusBar != null
                    && sTmpRect.setIntersect(win.getFrame(), mStatusBar.getFrame())
                    && !mStatusBarBackgroundCheckedBounds.contains(sTmpRect)) {
                mStatusBarBackgroundWindows.add(win);
                mStatusBarBackgroundCheckedBounds.union(sTmpRect);
                if (!mStatusBarColorCheckedBounds.contains(sTmpRect)) {
                    mStatusBarAppearanceRegionList.add(new AppearanceRegion(
                            win.mAttrs.insetsFlags.appearance & APPEARANCE_LIGHT_STATUS_BARS,
                            new Rect(win.getFrame())));
                    mStatusBarColorCheckedBounds.union(sTmpRect);
                }
            }
        }
    //省略若干...
}

WindowManager.LayoutParams

        /**
         * @hide
         * @return True if the layout parameters will cause the window to cover the full screen;
         *         false otherwise.
         */
        public boolean isFullscreen() {
            return x == 0 && y == 0
                    && width == WindowManager.LayoutParams.MATCH_PARENT
                    && height == WindowManager.LayoutParams.MATCH_PARENT;
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容