public static void setStatusBarColor(Activity activity, int statusColor) {
    Window window = activity.getWindow();
    ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      // First translucent status bar.
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // After LOLLIPOP not translucent status bar
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // Then call setStatusBarColor.
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(statusColor);
        // set child View not fill the system window
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
          ViewCompat.setFitsSystemWindows(mChildView, true);
        }
      } else {
        int statusBarHeight = getStatusBarHeight(activity);

        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
          FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
          // if margin top has already set, just skip.
          if (lp != null && lp.topMargin < statusBarHeight && lp.height != statusBarHeight) {
            // do not use fitsSystemWindows
            ViewCompat.setFitsSystemWindows(mChildView, false);
            // add margin to content
            lp.topMargin += statusBarHeight;
            mChildView.setLayoutParams(lp);
          }
        }

        // Before LOLLIPOP create a fake status bar View.
        View statusBarView = mContentView.getChildAt(0);
        if (statusBarView != null
            && statusBarView.getLayoutParams() != null
            && statusBarView.getLayoutParams().height == statusBarHeight) {
          // if fake status bar view exist, we can setBackgroundColor and return.
          statusBarView.setBackgroundColor(statusColor);
          return;
        }
        statusBarView = new View(activity);
        ViewGroup.LayoutParams lp =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        statusBarView.setBackgroundColor(statusColor);
        mContentView.addView(statusBarView, 0, lp);
      }
    }
  }
  /**
   * change to full screen mode
   *
   * @param hideStatusBarBackground hide status bar Background when SDK > 21, true if hide it
   */
  public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
    Window window = activity.getWindow();
    ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

    // set child View not fill the system window
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
      ViewCompat.setFitsSystemWindows(mChildView, false);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      int statusBarHeight = getStatusBarHeight(activity);

      // First translucent status bar.
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // After LOLLIPOP just set LayoutParams.
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        if (hideStatusBarBackground) {
          window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          window.setStatusBarColor(Color.parseColor("#00000000"));
        } else {
          window.setStatusBarColor(Color.parseColor("#55000000"));
        }
        // must call requestLayout, otherwise it will have space in screen bottom
        if (mChildView != null) {
          ViewCompat.requestApplyInsets(mChildView);
        }
      } else {
        if (mChildView != null
            && mChildView.getLayoutParams() != null
            && mChildView.getLayoutParams().height == statusBarHeight) {
          // Before LOLLIPOP need remove fake status bar view.
          mContentView.removeView(mChildView);
          mChildView = mContentView.getChildAt(0);
        }
        if (mChildView != null) {
          FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
          // cancel the margin top
          if (lp != null && lp.topMargin >= statusBarHeight) {
            lp.topMargin -= statusBarHeight;
            mChildView.setLayoutParams(lp);
          }
        }
      }
    }
  }
Пример #3
0
  /**
   * snippet from https://github.com/niorgai/StatusBarCompat
   *
   * <p>直接使用会引起NavigationView上面空白一部分,需要将其margin - statusBarHeight
   */
  @TargetApi(19)
  private static void tianKeng(Activity activity, int statusColor, NavigationView navigationView) {
    Window window = activity.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    int statusBarHeight = getStatusBarHeight(activity);

    ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
      FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
      // if margin top has already set, just skip.
      if (lp != null && lp.topMargin < statusBarHeight && lp.height != statusBarHeight) {
        // do not use fitsSystemWindows
        ViewCompat.setFitsSystemWindows(mChildView, false);
        // add margin to content
        lp.topMargin += statusBarHeight;
        mChildView.setLayoutParams(lp);
      }
    }

    View statusBarView = mContentView.getChildAt(0);
    if (statusBarView != null
        && statusBarView.getLayoutParams() != null
        && statusBarView.getLayoutParams().height == statusBarHeight) {
      // if fake status bar view exist, we can setBackgroundColor and return.
      statusBarView.setBackgroundColor(statusColor);
      return;
    }
    statusBarView = new View(activity);
    ViewGroup.LayoutParams lp =
        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
    statusBarView.setBackgroundColor(statusColor);
    mContentView.addView(statusBarView, 0, lp);

    DrawerLayout.LayoutParams nvLp = (DrawerLayout.LayoutParams) navigationView.getLayoutParams();
    nvLp.topMargin -= statusBarHeight;
    navigationView.setLayoutParams(nvLp);
  }