コード例 #1
0
  /**
   * Constructor. Call this in the host activity onCreate method after its content view has been
   * set. You should always create new instances when the host activity is recreated.
   *
   * @param activity The host activity.
   */
  @TargetApi(19)
  public SystemBarTintManager(Activity activity) {

    Window win = activity.getWindow();
    ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      // check theme attrs
      int[] attrs = {
        android.R.attr.windowTranslucentStatus, android.R.attr.windowTranslucentNavigation
      };
      TypedArray a = activity.obtainStyledAttributes(attrs);
      try {
        mStatusBarAvailable = a.getBoolean(0, false);
        mNavBarAvailable = a.getBoolean(1, false);
      } finally {
        a.recycle();
      }

      // check window flags
      WindowManager.LayoutParams winParams = win.getAttributes();
      int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
      if ((winParams.flags & bits) != 0) {
        mStatusBarAvailable = true;
      }
      bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
      if ((winParams.flags & bits) != 0) {
        mNavBarAvailable = true;
      }
    }

    mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);
    // device might not have virtual navigation keys
    if (!mConfig.hasNavigtionBar()) {
      mNavBarAvailable = false;
    }

    if (mStatusBarAvailable) {
      setupStatusBarView(activity, decorViewGroup);
    }
    if (mNavBarAvailable) {
      setupNavBarView(activity, decorViewGroup);
    }
  }