How to get Toolbar from fragment?

后端 未结 10 1848
梦毁少年i
梦毁少年i 2020-11-28 01:45

I have ActionBarActivity with NavigationDrawer and use support_v7 Toolbar as ActionBar. In one of my fragments toolbar has custom view

10条回答
  •  长情又很酷
    2020-11-28 02:26

    From your Fragment: ( get Toolbar from fragment?)

    // get toolbar
    ((MainAcivity)this.getActivity()).getToolbar();  // getToolbar will be method in Activity that returns Toolbar!!  don't use getSupportActionBar for getting toolbar!!
    // get action bar
    this.getActivity().getSupportActionBar();
    

    this is very helpful when you are using spinner in Toolbar and call the spinner or custom views in Toolbar from a fragment!

    From your Activity:

    // get toolbar
    this.getToolbar();
    // get Action Bar
    this.getSupportActionBar();
    

提交回复
热议问题