示例#1
0
文件: Main.java 项目: lucEvent/NewsUp
  private void setUpColors(int colorCode) {
    int barColor, textColor, statusBarColor;
    boolean navBlack = false;

    Toolbar ab = (Toolbar) findViewById(R.id.toolbar);
    if (colorCode < 0) {
      barColor = ContextCompat.getColor(this, R.color.colorPrimary);
      textColor = Color.WHITE;
      statusBarColor = ContextCompat.getColor(this, R.color.colorPrimaryDark);
    } else {
      Site site = AppData.getSiteByCode(colorCode);

      barColor = site.color;
      textColor = site.hasDarkColor() ? Color.WHITE : Color.BLACK;
      statusBarColor = barColor == 0xffffffff ? 0xffcccccc : barColor;
      navBlack = !site.hasDarkColor();
    }
    //noinspection ConstantConditions
    ab.setBackgroundColor(barColor);
    ab.setTitleTextColor(textColor);

    if (!navBlack)
      //noinspection ConstantConditions
      ab.getNavigationIcon().clearColorFilter();
    else
      //noinspection ConstantConditions
      ab.getNavigationIcon().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      if (statusBarColor == 0xffcccccc) {
        drawer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        statusBarColor = barColor;
      } else drawer.setSystemUiVisibility(0);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      Window window = getWindow();
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.setStatusBarColor(statusBarColor);
    }
  }