Set up toolbar as actionbar in fragment

前端 未结 6 1755
天命终不由人
天命终不由人 2021-02-01 01:51

I want to set up my toolbar as an actionbar, but since your toolbar is a layoutelement it has to be in your layout. Now my layout is in my fragment.

I added the toolbar

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 02:11

    Lets say the Activity holding the fragment is MainActivity.

    Do

    MainActivity main = (MainActivity)getActivity();
    //You can access all public variable and methods of MainActivity.
    //simply call 
    main.setSupportActionBar(toolbar)
    main.getSupportActionBar.setTitle("title");
    

提交回复
热议问题