溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android SystemBar如何使用

發(fā)布時(shí)間:2022-03-16 13:42:01 來源:億速云 閱讀:169 作者:iii 欄目:web開發(fā)

今天小編給大家分享一下Android SystemBar如何使用的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

ROM 去掉NavigationBar

resgrep NavigationBar相關(guān)的res,發(fā)現(xiàn)在framework/base/core/res/res/config.xml中可以通過config_showNavigationBar配置,繼續(xù)jgrep showNavigationBar相關(guān)的java,發(fā)現(xiàn)在PhoneWindowManager中通過boolean變量mHasNavigationBar記錄是否有NavigationBar,具體代碼如下:

mHasNavigationBar = res.getBoolean(com.android.internal.R.bool.config_showNavigationBar);

需要注意的是mHasNavigationBar也可以通過系統(tǒng)提供的Property來標(biāo)識,具體代碼如下:

String navBarOverride = SystemProperties.get("qemu.hw.mainkeys");

if ("1".equals(navBarOverride)) {

    mHasNavigationBar = false;

} else if ("0".equals(navBarOverride)) {

    mHasNavigationBar = true;

}

在PhoneWindowManager中存在一個(gè)類型為WindowState的變量mNavigationBar,WindowState是在PhoneWindowManger prepareAddWindow時(shí)會用到的一個(gè)interface,仔細(xì)看看prepareAddWindowLw方法

switch (attrs.type) {

    case TYPE_NAVIGATION_BAR:

       mContext.enforceCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR_SERVICE, "PhoneWindowManager");

        if (mNavigationBar != null) {

            if (mNavigationBar.isAlive()) {

                return WindowManagerGlobal.ADD_MULTIPLE_SINGLETON;

            }

        }

        mNavigationBar = win;

        mNavigationBarController.setWindow(win);

        if (DEBUG_LAYOUT) Slog.i(TAG, "NAVIGATION BAR: " + mNavigationBar);

       break;

}

判斷type為TYPE_NAVIGATION_BAR的時(shí)候,給mNavigationBar賦值,并且mNavigationBarController setWindow,mNavigationBarController是一個(gè)用來操縱SystemBar Window的類,有興趣可以詳細(xì)看看這個(gè)類,位于/framework/base/services/core/java/com/android/server/policy。繼續(xù)瀏覽代碼會發(fā)現(xiàn)方法layoutNavigationBar,通過mNavigationBarController對mNavigationBar進(jìn)行l(wèi)ayout。

在framework/base/packages/SystemUI中PhoneStatusBar中makeStatusBarView有如下一段代碼:

try {

    boolean showNav = mWindowManagerService.hasNavigationBar();

    if (DEBUG) Log.v(TAG, "hasNavigationBar=" + showNav);

    if (showNav) {

        createNavigationBarView(context);

    }

} catch (RemoteException ex) {

    // no window manager? good luck with that

}

根據(jù)WindowManagerService中的hasNavigationBar方法判斷是否有NavigationBar,再看WindowMangerService中是調(diào)用了PhoneWindowManager中的hasNavigationBar方法,最終返回的就是最開始提到了mHasNavigationBar這個(gè)標(biāo)識。在PhoneStatusBar有詳細(xì)講到如何createAndAddWindows,感興趣可以再進(jìn)一步了解。

SystemBar Translucent支持

Android在API 19的時(shí)候添加了兩個(gè)WindowManager.LayoutParams,分別是FLAG_TRANSLUCENT_NAVIGATION、FLAG_TRANSLUCENT_STATUS。

在官方文檔中有提到當(dāng)為Window設(shè)置FLAG_TRANSLUCENT_NAVIGATION時(shí),自動(dòng)設(shè)置了System UI visibility SYSTEM_UI_FLAG_LAYOUT_STABLE、SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION,當(dāng)為Window設(shè)置了FLAG_TRANSLUCENT_STATUS時(shí),自動(dòng)設(shè)置了System UI visibility SYSTEM_UI_FLAG_LAYOUT_STABLE、SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN。

顯然,這時(shí)System UI visibility有發(fā)生變化,在PhoneWindowManager中有如下一段代碼:

public int adjustSystemUiVisibilityLw(int visibility) {

        mStatusBarController.adjustSystemUiVisibilityLw(mLastSystemUiFlags, visibility);

        mNavigationBarController.adjustSystemUiVisibilityLw(mLastSystemUiFlags, visibility);

        mRecentsVisible = (visibility & View.RECENT_APPS_VISIBLE) > 0;

        mTvPictureInPictureVisible = (visibility & View.TV_PICTURE_IN_PICTURE_VISIBLE) > 0;

        // Reset any bits in mForceClearingStatusBarVisibility that

        // are now clear.

        mResettingSystemUiFlags &= visibility;

        // Clear any bits in the new visibility that are currently being

        // force cleared, before reporting it.

        return visibility & ——mResettingSystemUiFlags

            & ——mForceClearedSystemUiFlags;

}

可以看到前邊提到的操控SystemBar的Controller這個(gè)時(shí)候發(fā)揮了作用,調(diào)用了其adjustSystemUiVisibilityLw,繼續(xù)看BarController中相關(guān)代碼

public void adjustSystemUiVisibilityLw(int oldVis, int vis) {

    if (mWin != null && mTransientBarState == TRANSIENT_BAR_SHOWING && (vis & mTransientFlag) == 0) {

        // sysui requests hide

        setTransientBarState(TRANSIENT_BAR_HIDING);

        setBarShowingLw(false);

    } else if (mWin != null && (oldVis & mUnhideFlag) != 0 && (vis & mUnhideFla == 0) {

        // sysui ready to unhide

        setBarShowingLw(true);

    }

}

同時(shí),看到BarController中有applyTranslucentFlagLw,通過查找其調(diào)用處,發(fā)現(xiàn)依舊是在PhoneWindowManager中調(diào)用,以下是調(diào)用部分

// apply translucent bar vis flags

WindowState fullscreenTransWin = isStatusBarKeyguard() && !mHideLockScreen ? mStatusBar : mTopFullscreenOpaqueWindowState;

vis = mStatusBarController.applyTranslucentFlagLw(fullscreenTransWin, vis, oldVs);

vis = mNavigationBarController.applyTranslucentFlagLw(fullscreenTransWin, vis, oldVis);

final int dockedVis = mStatusBarController.applyTranslucentFlagLw(mTopDockedOpaqueWindowState, 0, 0);

final boolean fullscreenDrawsStatusBarBackground =

        (drawsSystemBarBackground(mTopFullscreenOpaqueWindowState) && (vis & View.STATUS_BAR_TRANSLUCENT) == 0) || forcesDrawStatusBarBackground(mTopFullscreenOpaqueWindowState);

final boolean dockedDrawsStatusBarBackground = (drawsSystemBarBackground(mTopDockedOpaqueWindowState) && (dockedVis & View.STATUS_BAR_TRANSLUCENT) == 0) || forcesDrawStatusBarBackground(mTopDockedOpaqueWindowState);

可以看到在PhoneWindowManager中通過updateSystemBarsLw進(jìn)行了相應(yīng)的更新SystemBars。這個(gè)地方還有一個(gè)比較有意思的問題是,在實(shí)際使用這兩個(gè)Flag的時(shí)候,會發(fā)現(xiàn)App自動(dòng)延伸到StatusBar和NavigationBar下方,這是為何?

猜想,應(yīng)該是App區(qū)域?qū)ο到y(tǒng)下發(fā)的WindowInsets值做了處理,查看ActionBarOverlayLayout onApplyWindowInsets方法,可以發(fā)現(xiàn)通過computeFitSystemWindows對ActionBarOverlayLayout的measure時(shí)用到的insets值做了處理,查看View的computeFitSystemWindows方法

protected boolean computeFitSystemWindows(Rect inoutInsets, Rect outLocalInsets) {

    if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0 || mAttachInfo == null || ((mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0 && !mAttachInfo.mOverscanRequested)) {

        outLocalInsets.set(inoutInsets);

        inoutInsets.set(0, 0, 0, 0);

        return true;

    } else {

        // The application wants to take care of fitting system window for

        // the content...  however we still need to take care of any overscan here.

        final Rect overscan = mAttachInfo.mOverscanInsets;

        outLocalInsets.set(overscan);

        inoutInsets.left -= overscan.left;

        inoutInsets.top -= overscan.top;

        inoutInsets.right -= overscan.right;

        inoutInsets.bottom -= overscan.bottom;

        return false;

    }

}

可以發(fā)現(xiàn)這個(gè)地方,根據(jù)flag的判斷通過overscan Rect對ActionBarOverlayLayout的insets值進(jìn)行了修改(最近把這個(gè)處理移植到API 19以下了,嘿嘿嘿)。當(dāng)然Google也沒勇氣對這個(gè)處理一鍋端,因此App可以使用fitsSystemWindows屬性來避免系統(tǒng)做這個(gè)處理,通過這個(gè)屬性可以默認(rèn)把insets值給到當(dāng)前View的padding值,當(dāng)然App也可以自己繼承View的fitSystemWindows方法來自己處理insets。

SystemBar setColor支持

Android在API 21的時(shí)候?yàn)閃indow添加了setNavigationBarColor、setStatusBarColor,進(jìn)一步提升SystemBar用戶體驗(yàn)。

PhoneWindow繼承Window具體實(shí)現(xiàn)了setNavigationBarColor、setStatusBarColor,具體代碼如下:

public void setStatusBarColor(int color) {

    mStatusBarColor = color;

    mForcedStatusBarColor = true;

    if (mDecor != null) {

        mDecor.updateColorViews(null, false /* animate */);

    }

}

public void setNavigationBarColor(int color) {

    mNavigationBarColor = color;

    mForcedNavigationBarColor = true;

    if (mDecor != null) {

        mDecor.updateColorViews(null, false /* animate */);

        mDecor.updateNavigationGuardColor();

    }

}

不難發(fā)現(xiàn)主要是DecorView的updateColorViews在work,通過查看代碼,可以明白是DecorView在SystemBar的位置add了對應(yīng)的ColorStateView,這個(gè)有點(diǎn)類似PhoneWindowManager里邊的WindowState,之后對ColotStateView里邊的view進(jìn)行操作即可,比如說setBackground來改變其顏色。

以上就是“Android SystemBar如何使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI