コレクションコード-Androidステータスバーツールコード

4739 ワード

コレクションコード-Androidステータスバーツールコード
      
   */
  protected void setStatusBarFullTransparent() {
      if (Build.VERSION.SDK_INT >= 21) {//21  5.0
          Window window = getWindow();
          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 >= 19) {//19  4.4
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          //       
          //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      }
  }

  /**
   *       
   */
  protected void setHalfTransparent() {

      if (Build.VERSION.SDK_INT >= 21) {//21  5.0
          View decorView = getWindow().getDecorView();
          int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
          decorView.setSystemUiVisibility(option);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

      } else if (Build.VERSION.SDK_INT >= 19) {//19  4.4
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          //       
          // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      }
  }

  /**
   *          StatusBar
   *       xml     ,     fitsSystemWindows=true。
   */
  private View contentViewGroup;

  protected void setFitSystemWindow(boolean fitSystemWindow) {
      if (contentViewGroup == null) {
          contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
      }
      contentViewGroup.setFitsSystemWindows(fitSystemWindow);
  }

  /**
   *     4.4     ->     
   */
  protected void setDrawerLayoutFitSystemWindow() {
      if (Build.VERSION.SDK_INT == 19) {//19  4.4
          int statusBarHeight = getStatusHeight(this);
          if (contentViewGroup == null) {
              contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
          }
          if (contentViewGroup instanceof DrawerLayout) {
              DrawerLayout drawerLayout = (DrawerLayout) contentViewGroup;
              drawerLayout.setClipToPadding(true);
              drawerLayout.setFitsSystemWindows(false);
              for (int i = 0; i < drawerLayout.getChildCount(); i++) {
                  View child = drawerLayout.getChildAt(i);
                  child.setFitsSystemWindows(false);
                  child.setPadding(0,statusBarHeight, 0, 0);
              }

          }
      }
  }