Android4.4以上沉浸式狀態(tài)欄的簡(jiǎn)單適配

先看下通用的代碼

 // 5.0以上系統(tǒng)狀態(tài)欄透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

            int statusBarHeight1 = -1;
            //獲取status_bar_height資源的ID
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根據(jù)資源ID獲取響應(yīng)的尺寸值
                statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
            }

            //設(shè)置contentview為fitsSystemWindows
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
            //給statusbar著色
            View view = new View(this);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight1));
            view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            contentView.addView(view);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            int statusBarHeight1 = -1;
            //獲取status_bar_height資源的ID
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根據(jù)資源ID獲取響應(yīng)的尺寸值
                statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
            }

            //設(shè)置contentview為fitsSystemWindows
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View childAt = contentView.getChildAt(0);
            if (childAt != null) {
                childAt.setFitsSystemWindows(true);
            }
            //給statusbar著色
            View view = new View(this);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight1));
            view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            contentView.addView(view);
        }

對(duì)于5.0以上的系統(tǒng),狀態(tài)欄可以直接調(diào)用API去實(shí)現(xiàn)。

getWindow().setStatusBarColor(getResources().getColor(R.color.trans));

對(duì)于6.0以上的系統(tǒng),部分機(jī)型可以直接調(diào)用API去改變狀態(tài)欄圖標(biāo)的顏色。

?著作權(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)容