Android沉浸式狀態(tài)欄/狀態(tài)欄設置背景圖片/自定義漸變式狀態(tài)欄

先圖為敬。

1.普通效果

普通效果.jpg

2.狀態(tài)欄為背景圖片效果

狀態(tài)欄為圖片效果.jpg

3.側滑頁面效果

側滑頁面效果.jpg

4.自定義漸變效果

自定義漸變效果.jpg

5.含ActionBar效果

含ActionBar效果.jpg

工具類

package 包名;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;


public class StatusBarUtils {
    private Activity mActivity;
    //狀態(tài)欄顏色
    private int mColor = -1;
    //狀態(tài)欄drawble
    private Drawable mDrawable;
    //是否是最外層布局是 DrawerLayout 的側滑菜單
    private boolean mIsDrawerLayout;
    //是否包含 ActionBar
    private boolean mIsActionBar;
    //側滑菜單頁面的內(nèi)容視圖
    private int mContentResourseIdInDrawer;

    public StatusBarUtils(Activity activity) {
        mActivity = activity;
    }

    public static StatusBarUtils with(Activity activity) {
        return new StatusBarUtils(activity);
    }

    public int getColor() {
        return mColor;
    }

    public StatusBarUtils setColor(int color) {
        mColor = color;
        return this;
    }

    public Drawable getDrawable() {
        return mDrawable;
    }

    public StatusBarUtils setDrawable(Drawable drawable) {
        mDrawable = drawable;
        return this;
    }

    public boolean isDrawerLayout() {
        return mIsDrawerLayout;
    }

    public boolean isActionBar() {
        return mIsActionBar;
    }

    public StatusBarUtils setIsActionBar(boolean actionBar) {
        mIsActionBar = actionBar;
        return this;
    }

    /**
     * 是否是最外層布局為 DrawerLayout 的側滑菜單
     *
     * @param drawerLayout 是否最外層布局為 DrawerLayout
     * @param contentId    內(nèi)容視圖的 id
     * @return
     */
    public StatusBarUtils setDrawerLayoutContentId(boolean drawerLayout, int contentId) {
        mIsDrawerLayout = drawerLayout;
        mContentResourseIdInDrawer = contentId;
        return this;
    }

    public void init() {
        fullScreen(mActivity);
        if (mColor != -1) {
            //設置了狀態(tài)欄顏色
            addStatusViewWithColor(mActivity, mColor);
        }
        if (mDrawable != null) {
            //設置了狀態(tài)欄 drawble,例如漸變色
            addStatusViewWithDrawble(mActivity, mDrawable);
        }
        if (isDrawerLayout()) {
            //未設置 fitsSystemWindows 且是側滑菜單,需要設置 fitsSystemWindows 以解決 4.4 上側滑菜單上方白條問題
            fitsSystemWindows(mActivity);
        }
        if (isActionBar()) {
            //要增加內(nèi)容視圖的 paddingTop,否則內(nèi)容被 ActionBar 遮蓋
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                ViewGroup rootView = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(android.R.id.content);
                rootView.setPadding(0, getStatusBarHeight(mActivity) + getActionBarHeight(mActivity), 0, 0);
            }
        }
    }

    /**
     * 去除 ActionBar 陰影
     */
    public StatusBarUtils clearActionBarShadow() {
        if (Build.VERSION.SDK_INT >= 21) {
            ActionBar supportActionBar = ((AppCompatActivity) mActivity).getSupportActionBar();
            if (supportActionBar != null) {
                supportActionBar.setElevation(0);
            }
        }
        return this;
    }

    /**
     * 設置頁面最外層布局 FitsSystemWindows 屬性
     *
     * @param activity
     */
    private void fitsSystemWindows(Activity activity) {
        ViewGroup contentFrameLayout = (ViewGroup) activity.findViewById(android.R.id.content);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
            //布局預留狀態(tài)欄高度的 padding
            if (parentView instanceof DrawerLayout) {
                DrawerLayout drawer = (DrawerLayout) parentView;
                //將主頁面頂部延伸至status bar;雖默認為false,但經(jīng)測試,DrawerLayout需顯示設置
                drawer.setClipToPadding(false);
            }
        }
    }

    /**
     * 利用反射獲取狀態(tài)欄高度
     *
     * @return
     */
    public static int getStatusBarHeight(Activity activity) {
        int result = 0;
        //獲取狀態(tài)欄高度的資源id
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = activity.getResources().getDimensionPixelSize(resourceId);
        }
        Log.e("getStatusBarHeight", result + "");
        return result;
    }

    /**
     * 獲得 ActionBar 的高度
     *
     * @param context
     * @return
     */
    public static int getActionBarHeight(Context context) {
        int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            TypedValue tv = new TypedValue();
            context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
            result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
        return result;
    }

    /**
     * 添加狀態(tài)欄占位視圖
     *
     * @param activity
     */
    private void addStatusViewWithColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (isDrawerLayout()) {
                //要在內(nèi)容布局增加狀態(tài)欄,否則會蓋在側滑菜單上
                ViewGroup rootView = (ViewGroup) activity.findViewById(android.R.id.content);
                //DrawerLayout 則需要在第一個子視圖即內(nèi)容試圖中添加padding
                View parentView = rootView.getChildAt(0);
                LinearLayout linearLayout = new LinearLayout(activity);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                View statusBarView = new View(activity);
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        getStatusBarHeight(activity));
                statusBarView.setBackgroundColor(color);
                //添加占位狀態(tài)欄到線性布局中
                linearLayout.addView(statusBarView, lp);
                //側滑菜單
                DrawerLayout drawer = (DrawerLayout) parentView;
                //內(nèi)容視圖
                View content = activity.findViewById(mContentResourseIdInDrawer);
                //將內(nèi)容視圖從 DrawerLayout 中移除
                drawer.removeView(content);
                //添加內(nèi)容視圖
                linearLayout.addView(content, content.getLayoutParams());
                //將帶有占位狀態(tài)欄的新的內(nèi)容視圖設置給 DrawerLayout
                drawer.addView(linearLayout, 0);
            } else {
                //設置 paddingTop
                ViewGroup rootView = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(android.R.id.content);
                rootView.setPadding(0, getStatusBarHeight(mActivity), 0, 0);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    //直接設置狀態(tài)欄顏色
                    activity.getWindow().setStatusBarColor(color);
                } else {
                    //增加占位狀態(tài)欄
                    ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
                    View statusBarView = new View(activity);
                    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            getStatusBarHeight(activity));
                    statusBarView.setBackgroundColor(color);
                    decorView.addView(statusBarView, lp);
                }
            }
        }
    }

    /**
     * 添加狀態(tài)欄占位視圖
     *
     * @param activity
     */
    private void addStatusViewWithDrawble(Activity activity, Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //占位狀態(tài)欄
            View statusBarView = new View(activity);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(activity));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                statusBarView.setBackground(drawable);
            } else {
                statusBarView.setBackgroundDrawable(drawable);
            }
            if (isDrawerLayout()) {
                //要在內(nèi)容布局增加狀態(tài)欄,否則會蓋在側滑菜單上
                ViewGroup rootView = (ViewGroup) activity.findViewById(android.R.id.content);
                //DrawerLayout 則需要在第一個子視圖即內(nèi)容試圖中添加padding
                View parentView = rootView.getChildAt(0);
                LinearLayout linearLayout = new LinearLayout(activity);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                //添加占位狀態(tài)欄到線性布局中
                linearLayout.addView(statusBarView, lp);
                //側滑菜單
                DrawerLayout drawer = (DrawerLayout) parentView;
                //內(nèi)容視圖
                View content = activity.findViewById(mContentResourseIdInDrawer);
                //將內(nèi)容視圖從 DrawerLayout 中移除
                drawer.removeView(content);
                //添加內(nèi)容視圖
                linearLayout.addView(content, content.getLayoutParams());
                //將帶有占位狀態(tài)欄的新的內(nèi)容視圖設置給 DrawerLayout
                drawer.addView(linearLayout, 0);
            } else {
                //增加占位狀態(tài)欄,并增加狀態(tài)欄高度的 paddingTop
                ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
                decorView.addView(statusBarView, lp);
                //設置 paddingTop
                ViewGroup rootView = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(android.R.id.content);
                rootView.setPadding(0, getStatusBarHeight(mActivity), 0, 0);
            }
        }
    }

    /**
     * 通過設置全屏,設置狀態(tài)欄透明
     *
     * @param activity
     */
    private void fullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x開始需要把顏色設置透明,否則導航欄會呈現(xiàn)系統(tǒng)默認的淺灰色
                Window window = activity.getWindow();
                View decorView = window.getDecorView();
                //兩個 flag 要結合使用,表示讓應用的主體內(nèi)容占用系統(tǒng)狀態(tài)欄的空間
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                decorView.setSystemUiVisibility(option);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                //導航欄顏色也可以正常設置
//                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                attributes.flags |= flagTranslucentStatus;
//                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }

    /**
     * 通過設置全屏,設置狀態(tài)欄透明 導航欄黑色
     *
     * @param activity
     */
    public static void setStatusTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();

                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
//                attributes.flags |= flagTranslucentStatus;
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);

                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                attributes.flags |= flagTranslucentStatus;
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }
}

用法

1.普通效果

 //清單文件中要設置主題為NoActionBar
 StatusBarUtils.with(this)
                .setColor(getResources().getColor(R.color.blue))
                .init();

2.圖片背景效果

 //清單文件中要設置主題為NoActionBar
//在布局文件中設置的第一個view的背景圖片則為當前狀態(tài)欄背景圖片
 StatusBarUtils.with(this)
                .init();

3.側滑頁面狀態(tài)欄效果

 //清單文件中要設置主題為NoActionBar
//R.id.rl_content為主頁面布局
//代碼中設置的為主頁面狀態(tài)欄
//在側滑頁面布局文件中設置的第一個view的背景圖片則為側滑狀態(tài)欄背景圖片
      StatusBarUtils.with(this)
                .setDrawerLayoutContentId(true, R.id.rl_content)
                .setColor(getResources().getColor(R.color.blue))
                .init();

4.自定義漸變狀態(tài)欄

 //當前案例含ActionBar
    StatusBarUtils.with(this)
                .setIsActionBar(true)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(R.drawable.shape))
                .init();

自定義漸變shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="0"
        android:centerX="0.7"
        android:endColor="@color/shape2"
        android:startColor="@color/shape1"
        android:centerColor="@color/shape3"
        android:type="linear" />
</shape>
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。