关于状态栏的那些事

 /**
     * 淡化状态栏和导航栏
     * 用户触碰到状态栏或者导航栏时会自动恢复
     */
    public void danhuaClick(View view) {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        decorView.setSystemUiVisibility(uiOptions);
    }

    /**
     * 恢复淡化状态
     */
    public void huifuClick(View view) {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(0);
    }

    /**
     * 隐藏状态栏
     * 一旦设置会一直生效
     * 用户下拉时会显示状态栏,然后会自动隐藏状态栏
     */
    public void yincangztlClick(View view) {


        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

//            // 4.1之后可以动态隐藏状态栏,用户下拉后会恢复,需要重新设置
//            View decorView = getWindow().getDecorView();
//            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
//            decorView.setSystemUiVisibility(uiOptions);

    }

    /**
     * 隐藏导航栏和状态栏
     * 用户点击后重新显示
     */
    public void zhuangtaiClick(View view) {
        //4.0以上
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

    /**
     * 非粘性沉浸式模式
     * 用户向下滑动恢复,需重新设置才能生效
     * 恢复后Actionbar会盖住内容建议隐藏
     */
    public void feinianClick(View view) {
        View mDecorView = getWindow().getDecorView();
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }


    /**
     * 恢复非粘性沉浸式模式
     * 恢复后Actionbar会盖住内容建议隐藏
     */
    public void huifufeiClick(View view) {
        View mDecorView = getWindow().getDecorView();
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    /**
     * 粘性沉浸式模式
     * 用户下拉显示状态栏和导航栏
     * 一段时间后自动隐藏
     */
    public void nianxingClick(View view) {
        View mDecorView = getWindow().getDecorView();
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                        | View.SYSTEM_UI_FLAG_IMMERSIVE
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        );
    }

    /**
     * 内容穿透状态栏和导航栏
     * 为布局设置fitsSystemWindows属性防止内容位移
     * 根据具体情况为相应的控件设置此属性
     * 当actionbar存在时,谷歌并没有提供一个好的解决方案
     *
     * @param view
     */
    public void chuantouClick(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 5.0
            // 全透明状态栏
            getWindow().setStatusBarColor(Color.parseColor("#00000000"));
            // 半透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            // TODO 当窗口获取到焦点时会执行此方法
            Log.e("窗口", "获取焦点");
        }
    }

/**
     * 专注模式
     */
    public void myClick(View v){
        int currentVis = v.getSystemUiVisibility();
        int newVis;
        if ((currentVis & View.SYSTEM_UI_FLAG_LOW_PROFILE)
                == View.SYSTEM_UI_FLAG_LOW_PROFILE) {
            newVis = View.SYSTEM_UI_FLAG_VISIBLE;
        } else {
            newVis = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }
        v.setSystemUiVisibility(newVis);
    }
    /**
     * 隐藏底部导航栏
     *
     * @param v
     */
    public void myClick2(View v) {
        v.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    /**
     * 全屏模式
     */
    public void myClick3(View v) {
        v.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

你可能感兴趣的:(Android)