private void setToolbar() {
    Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            onBackPressed();
          }
        });

    ActionBar actionBar = getSupportActionBar();

    if (actionBar != null) {
      actionBar.setTitle(getString(R.string.settings));
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setDisplayShowHomeEnabled(true);
    }
    toolbar.setTitleTextAppearance(this, R.style.ToolbarTitleStyle);
    toolbar.setSubtitleTextAppearance(this, R.style.ToolbarSubtitleStyle_TextApperance);
  }
Exemplo n.º 2
0
  public void setToolbar(Toolbar toolbar) {
    toolbar.setTitleTextAppearance(getParentActivity(), R.style.ToolbarTitleStyle);
    toolbar.setSubtitleTextAppearance(
        getParentActivity(), R.style.ToolbarSubtitleStyle_TextApperance);

    getParentActivity().setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(
        new android.view.View.OnClickListener() {
          @Override
          public void onClick(android.view.View v) {
            getParentActivity().onBackPressed();
          }
        });

    ActionBar actionBar = getParentActivity().getSupportActionBar();

    if (actionBar != null && getParentActivity().getBackstackCount() > 0) {
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setDisplayShowHomeEnabled(true);
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    // 如果系统的主题为Activity_translucent_navigation_bar,但是手机没有navigation bar,则将其设置回status bar主题,
    // setTheme设置主题一定要在onCreate()之前
    if (!CommonUtils.hasNavigationBar()
        && getApplicationInfo().theme == R.style.Activity_translucent_navigation_bar) {
      setTheme(R.style.Activity_translucent_status_bar);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base_layout);
    fl_top_bar = (FrameLayout) findViewById(R.id.fl_top_bar);
    base_content = (FrameLayout) findViewById(R.id.base_content);
    sv_bottom_content = (ScrollView) findViewById(R.id.sv_bottom_content);
    ll_bottom_content = (LinearLayout) findViewById(R.id.ll_bottom_content);
    ll_full_screen = findViewById(R.id.ll_full_screen);
    ll_full_screen.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            doReverseAnimation();
          }
        });

    // SDK19版本以上
    if (Build.VERSION.SDK_INT >= 19
        && (getApplicationInfo().theme == R.style.Activity_translucent_status_bar
            || getApplicationInfo().theme == R.style.Activity_translucent_navigation_bar)) {
      v_status_bar = findViewById(R.id.v_status_bar);
      v_navigation_bar = findViewById(R.id.v_navigation_bar);
      v_status_bar.setVisibility(View.VISIBLE);
      v_navigation_bar.setVisibility(View.VISIBLE);

      int id = getResources().getIdentifier("status_bar_height", "dimen", "android");
      v_status_bar.getLayoutParams().height = getResources().getDimensionPixelOffset(id);
      // 如果手机有navigation bar
      if (CommonUtils.hasNavigationBar()) {
        isUsingNavigation =
            (getApplicationInfo().theme == R.style.Activity_translucent_navigation_bar);
        // 底部navigation bar透明风格
        if (isUsingNavigation) {
          id = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
          v_navigation_bar.getLayoutParams().height = getResources().getDimensionPixelOffset(id);
        }
      } else {
        v_navigation_bar.setVisibility(View.GONE);
        v_navigation_bar = null;
      }
    }

    // 添加top bar,现在有两种样式的top bar可以使用:
    // 一种是自定的viewGroup,比如QQ
    if (!useToolbar) {
      top_bar = (ViewGroup) View.inflate(this, R.layout.activity_top_bar_layout, null);
      View rl_back = top_bar.findViewById(R.id.rl_back);
      rl_back.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              finish();
            }
          });
      TextView tv_title = (TextView) top_bar.findViewById(R.id.tv_title);
      // 通过 android::label 设置的标题
      if (!TextUtils.isEmpty(getTitle())) tv_title.setText(getTitle());
    }
    // 一种是使用系统控件toolbar,比如微信
    else {
      top_bar = (ViewGroup) View.inflate(this, R.layout.activity_top_toolbar_layout, null);
      Toolbar toolbar = (Toolbar) top_bar;
      setSupportActionBar(toolbar);
      toolbar.setTitleTextAppearance(this, R.style.toolbar_title_appearance);
      toolbar.setTitleTextColor(getResources().getColor(R.color.white));
      toolbar.setNavigationIcon(R.mipmap.ic_arrow_back);
      toolbar.setNavigationOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              finish();
            }
          });
      // 通过 android::label 设置的标题
      if (!TextUtils.isEmpty(getTitle())) toolbar.setTitle(getTitle());
    }

    fl_top_bar.addView(top_bar);

    bottomItems = new LinkedHashMap<>();
    inflater = LayoutInflater.from(this);

    initView();
    initData();
  }
  public ToolbarWidgetWrapper(
      Toolbar toolbar,
      boolean style,
      int defaultNavigationContentDescription,
      int defaultNavigationIcon) {
    mToolbar = toolbar;
    mTitle = toolbar.getTitle();
    mSubtitle = toolbar.getSubtitle();
    mTitleSet = mTitle != null;
    mNavIcon = toolbar.getNavigationIcon();

    if (style) {
      final TintTypedArray a =
          TintTypedArray.obtainStyledAttributes(
              toolbar.getContext(), null, R.styleable.ActionBar, R.attr.actionBarStyle, 0);

      final CharSequence title = a.getText(R.styleable.ActionBar_title);
      if (!TextUtils.isEmpty(title)) {
        setTitle(title);
      }

      final CharSequence subtitle = a.getText(R.styleable.ActionBar_subtitle);
      if (!TextUtils.isEmpty(subtitle)) {
        setSubtitle(subtitle);
      }

      final Drawable logo = a.getDrawable(R.styleable.ActionBar_logo);
      if (logo != null) {
        setLogo(logo);
      }

      final Drawable icon = a.getDrawable(R.styleable.ActionBar_icon);
      if (mNavIcon == null && icon != null) {
        setIcon(icon);
      }

      final Drawable navIcon = a.getDrawable(R.styleable.ActionBar_homeAsUpIndicator);
      if (navIcon != null) {
        setNavigationIcon(navIcon);
      }

      setDisplayOptions(a.getInt(R.styleable.ActionBar_displayOptions, 0));

      final int customNavId = a.getResourceId(R.styleable.ActionBar_customNavigationLayout, 0);
      if (customNavId != 0) {
        setCustomView(
            LayoutInflater.from(mToolbar.getContext()).inflate(customNavId, mToolbar, false));
        setDisplayOptions(mDisplayOpts | ActionBar.DISPLAY_SHOW_CUSTOM);
      }

      final int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
      if (height > 0) {
        final ViewGroup.LayoutParams lp = mToolbar.getLayoutParams();
        lp.height = height;
        mToolbar.setLayoutParams(lp);
      }

      final int contentInsetStart =
          a.getDimensionPixelOffset(R.styleable.ActionBar_contentInsetStart, -1);
      final int contentInsetEnd =
          a.getDimensionPixelOffset(R.styleable.ActionBar_contentInsetEnd, -1);
      if (contentInsetStart >= 0 || contentInsetEnd >= 0) {
        mToolbar.setContentInsetsRelative(
            Math.max(contentInsetStart, 0), Math.max(contentInsetEnd, 0));
      }

      final int titleTextStyle = a.getResourceId(R.styleable.ActionBar_titleTextStyle, 0);
      if (titleTextStyle != 0) {
        mToolbar.setTitleTextAppearance(mToolbar.getContext(), titleTextStyle);
      }

      final int subtitleTextStyle = a.getResourceId(R.styleable.ActionBar_subtitleTextStyle, 0);
      if (subtitleTextStyle != 0) {
        mToolbar.setSubtitleTextAppearance(mToolbar.getContext(), subtitleTextStyle);
      }

      final int popupTheme = a.getResourceId(R.styleable.ActionBar_popupTheme, 0);
      if (popupTheme != 0) {
        mToolbar.setPopupTheme(popupTheme);
      }

      a.recycle();
      // Keep the TintManager in case we need it later
      mTintManager = a.getTintManager();
    } else {
      mDisplayOpts = detectDisplayOptions();
      // Create a TintManager in case we need it later
      mTintManager = new TintManager(toolbar.getContext());
    }

    setDefaultNavigationContentDescription(defaultNavigationContentDescription);
    mHomeDescription = mToolbar.getNavigationContentDescription();

    setDefaultNavigationIcon(mTintManager.getDrawable(defaultNavigationIcon));

    mToolbar.setNavigationOnClickListener(
        new View.OnClickListener() {
          final ActionMenuItem mNavItem =
              new ActionMenuItem(mToolbar.getContext(), 0, android.R.id.home, 0, 0, mTitle);

          @Override
          public void onClick(View v) {
            if (mWindowCallback != null && mMenuPrepared) {
              mWindowCallback.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, mNavItem);
            }
          }
        });
  }