Android 状态栏沉浸式或透明化最工具类

首先不管是沉浸式还是透明化的状态栏,网上众说纷纭,咱都不卵,咱只要求符合我的设计需求就OK。这些功能也有很多解决方案,笔者经过自己实践,把用于项目中的方案来告诉大家,一行代码帮你搞定头部图片延伸至状态栏或者改变状态栏的颜色透明度。工具类简单好用,还不用去每个根布局写fitsSystemWindows。适配4.4以上yo

先上图看效果,是你们想到的,那么可以妥妥的使用了!

图1:图片或内容延伸至状态看,IOS效果一致!
Android 状态栏沉浸式或透明化最工具类_第1张图片
360手机助手截图0805_09_24_01.png
图1:改变状态栏的颜色透明度。
Android 状态栏沉浸式或透明化最工具类_第2张图片
360手机助手截图0805_09_24_02.png

1、图片内容延伸至状态栏原理

首先把状态栏设置成透明的,然后利用topMargin,把内容延伸过去,easy吧!上代码:

/**
     * change to full screen mode
     * 头部是图片状态栏透明,内容延伸到状态栏情况使用
     * @param activity
     * @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
     */
    public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
        Window window = activity.getWindow();
        ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

        //set child View not fill the system window
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            ViewCompat.setFitsSystemWindows(mChildView, false);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = getStatusBarHeight(activity);

            //First translucent status bar.
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //After LOLLIPOP just set LayoutParams.
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                if (hideStatusBarBackground) {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.setStatusBarColor(COLOR_TRANSLUCENT);
                } else {
                    window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
                }
                //must call requestApplyInsets, otherwise it will have space in screen bottom
                if (mChildView != null) {
                    ViewCompat.requestApplyInsets(mChildView);
                }
            } else {
                ViewGroup mDecorView = (ViewGroup) window.getDecorView();
                if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean && (Boolean)mDecorView.getTag()) {
                    mChildView = mDecorView.getChildAt(0);
                    //remove fake status bar view.
                    mContentView.removeView(mChildView);
                    mChildView = mContentView.getChildAt(0);
                    if (mChildView != null) {
                        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
                        //cancel the margin top
                        if (lp != null && lp.topMargin >= statusBarHeight) {
                            lp.topMargin -= statusBarHeight;
                            mChildView.setLayoutParams(lp);
                        }
                    }
                    mDecorView.setTag(false);
                }
            }
        }
    }

下面改变状态栏颜色的就不多说,需要的小伙伴去这里查看完整demo:
https://github.com/shuijilove/SystemBarUtilDemo

你可能感兴趣的:(Android 状态栏沉浸式或透明化最工具类)