笔记:状态栏占位,以及隐藏5.0+半透明背景,加在非全屏,oncreate中,全屏切非全屏不抖动。

public void initStatusBar() {
    StatusBarUtil.setLightMode(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
    }

        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        // 获取状态栏高度
        int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        View rectView = new View(this);
        // 绘制一个和状态栏一样高的矩形,并添加到视图中
        LinearLayout.LayoutParams params
                = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        rectView.setLayoutParams(params);
        //设置状态栏颜色(该颜色根据你的App主题自行更改)
        rectView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        // 添加矩形View到布局中
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        decorView.addView(rectView);
        ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
        rootView.setFitsSystemWindows(true);
        rootView.setClipToPadding(true);
    }

你可能感兴趣的:(android)