Android 高版本实现沉浸式状态栏

目前实现的android高版本沉浸式状态栏分为两类:
1、是纯透明状态栏;
2、是纯透明状态栏,但是状态栏字体是黑色;

将状态栏的代码封装到BaseActivity中更方便使用:
BaseActivity:
 

public abstract class BaseActivity extends AppCompatActivity {
    BaseActivity baseActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTransparent();
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(initLayout());
        initView(savedInstanceState);
        setTabBar(setBarColor());
        initData();
        baseActivity = this;
    }

    private void setTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            getWindow().getDecorView().setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= 19) {//4.4全透明
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | params.flags;
        }
    }

    protected abstract int setBarColor();


    void setTabBar(int type) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (type == 1) {
                //纯透明黑字
                StatusBarLightMode(this);
            } else {
                //黑底白字
                SystemBarTintManager tintManager = new SystemBarTintManager(this);
                tintManager.setStatusBarTintEnabled(true);
                tintManager.setStatusBarTintResource(R.color.white);//通知栏所需颜色
            }
        }
    }

    /**
     * 设置状态栏黑色字体
     * 适配4.4以上版本MIUI、Flyme和6.0以上版本其他Android
     *
     * @param activity activity
     * @return 1:MIUUI 2:Flyme 3:android6.0
     */
    public static int StatusBarLightMode(Activity activity) {
        int result = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
                result = 2;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                result = 3;
            }
        }
        return result;
    }

    /**
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    private static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {
                result = false;
            }
        }
        return result;
    }

    //设置布局
    protected abstract int initLayout();

    protected abstract void initView(Bundle savedInstanceState);

    //初始化数据
    protected abstract void initData();
}

正常Activity继承之后重写 setBarColor 方法返回1就行,需要注意的是,这个方法需要在AndroidManifest方法里面将theme改成hideAppTheme;

hideAppTheme:

   <!-- Base application theme. -->
    <style name="HideAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        //这两个语句就是设置隐藏标题栏的语句
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

然后就可以直接使用了,效果图如下:

上一篇:Java Lambda表达式


下一篇:VS(Visual Studio)中查找项目里的中文字符