带 CustomView by Fragment 的工具栏

ToolBar with CustomView by Fragment

我有一个 ToolbarFragment 的 CustomView,所以当我为 Fragment A[=21= 设置 ToolBar ](工具栏已呈现)并尝试为 Fragment B I 更新它 没有得到任何更新(未呈现)

代码 :

private void setCustomToolBarForFragment() {
  Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);

  // set CustomView
  View customToolBar =
    getActivity().getLayoutInflater().inflate(R.layout.toolbar_custom_view, null);
  ((TextView) customToolBar.findViewById(R.id.tool_bat_title)).setText("Favourites");
  ((ImageView) customToolBar.findViewById(R.id.icon_left)).setImageResource(0);
  ((ImageView) customToolBar.findViewById(R.id.icon_right)).setImageResource(0);

  toolbar.addView(customToolBar);
  toolbar.invalidate();       // restore toolbar
  ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
}

尝试删除工具栏中已有的自定义视图。 toolbar.removeAllViews()toolbar.addView()

之前