Android 设置统一风格的状态栏

由于android4.4后才能设置状态栏,而且在android5.0前,状态栏都会覆盖一层灰色半透明的视图去不掉

所以为了统一状态栏的显示,判断在5.0后的手动给他加灰色半透明的view,跟qq的效果一样;

  private View statusBarview;//状态栏
    //color 状态栏的颜色,这里是(#7F999999写在colors.xml里后引用)

    private void setupStatusBar(@ColorRes int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//android
            setTransparentStatusbar(this);
            if (statusBarview == null) {
                statusBarview = new View(this);
                ViewGroup dview = (ViewGroup) getWindow().getDecorView();
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.getStatusBarHeight(this));
                dview.addView(statusBarview, layoutParams);
            }
            statusBarview.setBackgroundColor(ContextCompat.getColor(this, color));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(this, true);
        }
    }


 @TargetApi(21)
    public  void setTransparentStatusbar(Activity activity) {
        Window window = activity.getWindow();
//        window.requestFeature(Window.FEATURE_NO_TITLE);
        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);
    }
  @TargetApi(19)
    public void setTranslucentStatus(Activity  activity,boolean flag) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (flag) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }



猜你喜欢

转载自blog.csdn.net/ange_li/article/details/81040466