Пример #1
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);
  }