Android_全屏切换;沉侵式和全屏切换

1. 首先是全屏与非全屏的切换

Window方式

if (mIsFullScreen){//设置为非全屏
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setAttributes(lp);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}else{//设置为全屏
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    getWindow().setAttributes(lp);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}

view方式

if (mIsFullScreen){//设置为非全屏
    getWindow().getDecorView().
    setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}else{//设置为全屏
    getWindow().getDecorView().
    setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
2. 沉侵式和全屏的切换

沉侵式设置

private void setChenQinShi() {
      Window window = getWindow();
      // 设置状态栏全透明
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          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);
      } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      }
  }
/**
   * 得到状态栏 StatusBar 高度
   */
  public static int getStatusBarHeight(Context context) {
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
          return 0;
      } else {
          int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
          return context.getResources().getDimensionPixelSize(resourceId);
      }
  }

全屏代码设置

getWindow().getDecorView().
                        setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

你可能感兴趣的:(Android_全屏切换;沉侵式和全屏切换)