栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 8.1 如何动态修改导航栏宽度

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 8.1 如何动态修改导航栏宽度

需求:进入某个应用只保留左侧5个图标区域
代码路径:
frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
在PhoneWindowManager.java文件中beginLayoutLw()方法中修改,在每进入一个窗口的时候都会执行beginLayoutLw()方法,在该方法中会去重新初始化一下窗口的变量以及导航栏和状态栏,其中layoutNavigationBar初始化导航栏,layoutStatusBar初始化状态栏

@Override
public void beginLayoutLw(boolean isDefaultDisplay, int displayWidth, int displayHeight,
                          int displayRotation, int uiMode) {   
                boolean updateSysUiVisibility = layoutNavigationBar(displayWidth, displayHeight,
        displayRotation, uiMode, overscanLeft, overscanRight, overscanBottom, dcf, navVisible, navTranslucent,
        navAllowedHidden, statusBarExpandedNotKeyguard);
if (DEBUG_LAYOUT) Slog.i(TAG, String.format("mDock rect: (%d,%d - %d,%d)",
        mDockLeft, mDockTop, mDockRight, mDockBottom));
updateSysUiVisibility |= layoutStatusBar(pf, df, of, vf, dcf, sysui, isKeyguardShowing, displayRotation, uiMode);
if (updateSysUiVisibility) {
    updateSystemUiVisibilityLw();
}          
 }

在PhoneWindowManager.java文件中layoutNavigationBar方法中修改应用区域的宽度,right就是距离右边区域的宽度值

private boolean layoutNavigationBar(int displayWidth, int displayHeight, int displayRotation,
        int uiMode, int overscanLeft, int overscanRight, int overscanBottom, Rect dcf,
        boolean navVisible, boolean navTranslucent, boolean navAllowedHidden,
        boolean statusBarExpandedNotKeyguard) {
            if (mNavigationBar != null) {
        boolean transientNavBarShowing = mNavigationBarController.isTransientShowing();
        // Force the navigation bar to its appropriate place and
        // size.  We need to do this directly, instead of relying on
        // it to bubble up from the nav bar, because this needs to
        // change atomically with screen rotations.
        mNavigationBarPosition = navigationBarPosition(displayWidth, displayHeight,
                displayRotation);
        if (mNavigationBarPosition == NAV_BAR_BOTTOM) {
             top = displayHeight - overscanBottom
                    - getNavigationBarHeight(displayRotation, uiMode);
                mTmpNavigationframe.set(0, top, displayWidth, displayHeight - overscanBottom);
}
            if (mNaviHided) {  // SPRD: add for dynamic navigationbar
                mTmpNavigationframe.offset(0, getNavigationBarHeight(displayRotation, uiMode));
            }
            if(HIDE_BOTTOM_NAVI){
                mStableBottom = mStableFullscreenBottom = displayHeight;
            }else {
                mStableBottom = mStableFullscreenBottom = mTmpNavigationframe.top;
            }
            if (transientNavBarShowing) {
                mNavigationBarController.setBarShowingLw(true);
            } else if (navVisible) {
                mNavigationBarController.setBarShowingLw(true);
                mDockBottom = mTmpNavigationframe.top;
                mRestrictedScreenHeight = mDockBottom - mRestrictedScreenTop;
                mRestrictedOverscanScreenHeight = mDockBottom - mRestrictedOverscanScreenTop;
            } else {
                // We currently want to hide the navigation UI - unless we expanded the status
                // bar.
                mNavigationBarController.setBarShowingLw(statusBarExpandedNotKeyguard);
            }
            if (navVisible && !navTranslucent && !navAllowedHidden
                    && !mNavigationBar.isAnimatingLw()
                    && !mNavigationBarController.wasRecentlyTranslucent()) {
                // If the opaque nav bar is currently requested to be visible,
                // and not in the process of animating on or off, then
                // we can tell the app that it is covered by it.
                mSystemBottom = mTmpNavigationframe.top;
            }
        } else if (mNavigationBarPosition == NAV_BAR_RIGHT) {
            int left = displayWidth - overscanRight- getNavigationBarWidth(displayRotation, uiMode);
            mTmpNavigationframe.set(left, 0, displayWidth - overscanRight, displayHeight);
            if (mNaviHided) {  // SPRD: add for dynamic navigationbar
                mTmpNavigationframe.offset(getNavigationBarWidth(displayRotation, uiMode), 0);
            }
                mStableRight = mStableFullscreenRight = mTmpNavigationframe.left;
            if (transientNavBarShowing) {
                mNavigationBarController.setBarShowingLw(true);
            } else if (navVisible) {
                mNavigationBarController.setBarShowingLw(true);
                mDockRight = mTmpNavigationframe.left;
                mRestrictedScreenWidth = mDockRight - mRestrictedScreenLeft;
                mRestrictedOverscanScreenWidth = mDockRight - mRestrictedOverscanScreenLeft;
            } else {
                // We currently want to hide the navigation UI - unless we expanded the status
                // bar.
                mNavigationBarController.setBarShowingLw(statusBarExpandedNotKeyguard);
            }
            if (navVisible && !navTranslucent && !navAllowedHidden
                && !mNavigationBar.isAnimatingLw()
                && !mNavigationBarController.wasRecentlyTranslucent()) {
            // If the nav bar is currently requested to be visible,
            // and not in the process of animating on or off, then
            // we can tell the app that it is covered by it.
            mSystemRight = mTmpNavigationframe.left;
            }
        }else if (mNavigationBarPosition == NAV_BAR_LEFT) {
            int right = overscanLeft + getNavigationBarWidth(displayRotation, uiMode);
            if (SystemProperties.getBoolean("persist.sys.navigationbar.right",false)){
                right = 110;
            }
                mTmpNavigationframe.set(overscanLeft, 0, right, displayHeight);
                if (mNaviHided) {  // SPRD: add for dynamic navigationbar
                    mTmpNavigationframe.offset(-getNavigationBarWidth(displayRotation, uiMode), 0);
        }
        mStableLeft = mStableFullscreenLeft = mTmpNavigationframe.right;
        if (transientNavBarShowing) {
            mNavigationBarController.setBarShowingLw(true);
        } else if (navVisible) {
            mNavigationBarController.setBarShowingLw(true);
            mDockLeft = mTmpNavigationframe.right;
            // TODO: not so sure about those:
            mRestrictedScreenLeft = mRestrictedOverscanScreenLeft = mDockLeft;
            mRestrictedScreenWidth = mDockRight - mRestrictedScreenLeft;
            mRestrictedOverscanScreenWidth = mDockRight - mRestrictedOverscanScreenLeft;
        } else {
            // We currently want to hide the navigation UI - unless we expanded the status
            // bar.
            mNavigationBarController.setBarShowingLw(statusBarExpandedNotKeyguard);
        }
        if (navVisible && !navTranslucent && !navAllowedHidden
            && !mNavigationBar.isAnimatingLw()
            && !mNavigationBarController.wasRecentlyTranslucent()) {
            // If the nav bar is currently requested to be visible,
            // and not in the process of animating on or off, then
            // we can tell the app that it is covered by it.
            mSystemLeft = mTmpNavigationframe.right;
        }
    }
}
        }
        // Make sure the content and current rectangles are updated to
        // account for the restrictions from the navigation bar.
        mContentTop = mVoiceContentTop = mCurTop = mDockTop;
        mContentBottom = mVoiceContentBottom = mCurBottom = mDockBottom;
        mContentLeft = mVoiceContentLeft = mCurLeft = mDockLeft;
        mContentRight = mVoiceContentRight = mCurRight = mDockRight;
        mStatusBarLayer = mNavigationBar.getSurfaceLayer();
        // And compute the final frame.
        mNavigationBar.computeframeLw(mTmpNavigationframe, mTmpNavigationframe,
        mTmpNavigationframe, mTmpNavigationframe, mTmpNavigationframe, dcf,
        mTmpNavigationframe, mTmpNavigationframe);
        if (DEBUG_LAYOUT) Slog.i(TAG, "mNavigationBar frame: " + mTmpNavigationframe);
            if (mNavigationBarController.checkHiddenLw()) {
            return true;
    }
    return false;
 }

备注:主要控制应用区域的函数

mTmpNavigationframe.set(overscanLeft, 0, right, displayHeight);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号